Results 1 to 11 of 11
- 04-14-2014, 03:00 AM #1
Member
- Join Date
- Feb 2014
- Location
- Near Buffalo NY
- Posts
- 33
- Rep Power
- 0
Do I Need To Re-organize My Program?
I am getting to the end of my first java development. It occurs to me that maybe things are organized wrong because I just sort of stumbled in and let netbeans do things for me. This is how things are now.
I have three classes, call them A, B, and C. Just to give everyone an idea of the size, the netbeans line count for them is about 1458 for A, 186 for B and 327 for C.
In the present state I have created instances for classes B (b) and C (c) but I don't think I have an instance for class A. Class A includes the overall GUI and the main that basically says:
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new A().setVisible(true);
When I want to use methods of A in b and c, I keep hitting "Non-static method xx() cannot be referenced from static context". And something similar with some variables.
Is this thing organized wrong? How would a skilled java person organize this mess?
Thank In Advance,
Pete
- 04-14-2014, 03:09 AM #2
Re: Do I Need To Re-organize My Program?
"Non-static method xx() cannot be referenced from static context"
The only static method to use is the main() method. It should create an instance of the class and be done.If you don't understand my response, don't ignore it, ask a question.
- 04-14-2014, 03:53 AM #3
Member
- Join Date
- Feb 2014
- Location
- Near Buffalo NY
- Posts
- 33
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
I think I understand the static concept but not what is happening here. In A I have a method, say
public void amethod{....}.
If, in B I write code that says
A.amethod(). it gives me that static error.
If I change to
public static void amethod(){...},
clearly that is the wrong thing to do because any methods and variables of A used by amethod give me the opposite, "non-static cannot be referenced from static context".
So I guess I need an instance of A (call it a) so that I can do:
a.amethod(). Is that a good alternative?
Then create a new class "Starter" that basically does
A a=new A;
B b=new B;
C c=new C;
a.setVisible;
That seems like a major reorganozation that I could take forever to patch if it doesn't work or to find out that it won't work.
Am I barking up the wrong tree?
Pete
- 04-14-2014, 04:01 AM #4
Re: Do I Need To Re-organize My Program?
A.amethod()
To call a non-static method, you need a reference to the A class:
aRefToA.amethod();
a.amethod(). Is that a good alternative?
Perhaps a real code example would be better than this pseudo code.If you don't understand my response, don't ignore it, ask a question.
- 04-14-2014, 04:31 AM #5
Member
- Join Date
- Feb 2014
- Location
- Near Buffalo NY
- Posts
- 33
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
I think the pseudo code would be a lot worse. I have been very careful about saying the upper case are the class names and lower case are instances of the classes.
I think something is breaking into my head.
amethod
is method in A but it is not declared static.
When, creating B, I want to use amethod, I type:
A.amethod
somehow the compiler is saying that A.amethod is static.
I don't know why. Is it because A is the class not an instance of A?
I don't want to create an instance
A a=new A()
at this stage because, somehow there must be an instance of A started by the statement in A main that says:
new A.setVisible.
I say there must be an instance of A because a whole lot of fields/variables are alive and well, getting defined, manipulated, moved, etc.
I'll have to think about it overnight.
Thanks
- 04-14-2014, 10:56 AM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Do I Need To Re-organize My Program?
Better that you get some good sleep and think on it in the morning after a good breakfast ;)
For further assistance I would create some slimmed down mock versions of your classes as you have them now that you can show, then at least there is something visible to talk about."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 04-14-2014, 04:13 PM #7
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
To keep it simple, main() should be your only static method. But if all other methods are non-static, then to call them you need a reference to an instance (object), not just the class.
So instead of this:
A.setVisible(true);
You would have this:
A a = new A();
a.setVisible(true);
That may be a major reorganization but most likely it's the right thing to do.
But there's a surprise: what if B needs to call non-static methods in A? If this code were in B:
A a = new A();
a.setVisible(false);
It wouldn't touch the same instance 'a' that was set visible earlier, because it simply created its own new instance of A instead (also called 'a' but it doesn't matter, it's a different variable). The code in B needs a way to get ahold of the actual 'a' instance that was set visible earlier. Do that by adding a getter method in the class that created and owns 'a':
private A a;
public A getA() {
return a;
}
Now you just need to decide what classes should create and own what other classes.
- 04-14-2014, 05:20 PM #8
Member
- Join Date
- Feb 2014
- Location
- Near Buffalo NY
- Posts
- 33
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
BinaryDigit09,
I may be starting to understand this. You confirmed my suspicion that I might run the risk of having multiple instances of A and better yet, gave me a way around it.
A a = new A();
a.setVisible(true);
(1) But right now the A.setVisible statement is in the main of class A.
(2) The class A is the over all control thingy for my program and right now it has the statements,
B b = new B();
C c = new C();
So, I think the Class A should own everything. But can Class A own an instance A?
Is it legitimate for the main of class A to have the statement:
A a = new A(); ?
If I did this, then the class definition of A would have all the following statements:
*******************************************
public static void main (String[] args){
A a = new A();
a.setVisible(true);
}
private A a; (((doesn't this seem weird to be in class definition of A?)))
public A getA(){
return a;
}
********************************************
Also, I don't have any getters for b and c so either I am unknowingly playing with multiple copies of them or I dodged a silver bullet some how because it seems the variables have the correct values and change as they should. I have to go through my code with a fine tooth comb I guess.
Thanks again.
- 04-14-2014, 06:14 PM #9
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
(1) But right now the A.setVisible statement is in the main of class A.
(2) The class A is the over all control thingy for my program and right now it has the statements,
B b = new B();
C c = new C();
So, I think the Class A should own everything. But can Class A own an instance A?
Java Code:class A { private static A a; private B b; private C c; public A() { b = new B(); c = new C(); } public static void main(String[] args) { a = new A(); a.setVisible(true); } public static A getA() { return a; } public B getB() { return b; } public C getC() { return c; } }
- 04-14-2014, 11:47 PM #10
Member
- Join Date
- Feb 2014
- Location
- Near Buffalo NY
- Posts
- 33
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
OK, just to let you know I am alive and studying this.
It is really elegant. But I haven't figured it all out yet.
I think I will use it by rot to keep the project moving then come back and play with it a little.
There seems to be a whole lot of mysterious stuff going on in the background and I don't like using something I don't understand.
You may see some questions from me when I start experimenting.
Thanks
Pete
- 04-15-2014, 05:38 AM #11
Member
- Join Date
- Feb 2014
- Location
- Near Buffalo NY
- Posts
- 33
- Rep Power
- 0
Re: Do I Need To Re-organize My Program?
I'm having some fun implementing this. I guess I don't have a feel for when instances get created and for how long instances last.
In the class B I have one method, that needs to use one method in a and a second method that needs to use one method in c.
It looks like I need to put the A a=A.getA(); in both methods (of b). Once to "find" the method in a and a second time to find c to find its method.
I suppose that doesn't hurt because a is static is that right? So either A a=A.getA() just uses the same a. (I wish I had used x,y,z not a,b,c as examples).
What about the C c=new a.getC(): ? Can they be splashed around in many methods without causing problems? I think I read somewhere that java does its own cleanup on the fly.
Other then that, everything seems to work.
It looks like your technique is excellent.
You should document it or put it in a sticky.
Pete
Similar Threads
-
Trying to figure out the the best way to organize classes for Uno card game
By BustTheCode in forum New To JavaReplies: 8Last Post: 03-22-2014, 03:06 AM -
How to organize/design a date and time array?
By RORE in forum New To JavaReplies: 7Last Post: 07-05-2011, 11:44 AM -
Problem to organize my code in classes
By ze snow in forum New To JavaReplies: 4Last Post: 02-23-2010, 05:28 AM -
Organize class source code
By Alejandro Valdez in forum EclipseReplies: 0Last Post: 05-16-2008, 03:58 PM
Bookmarks