Results 1 to 11 of 11
  1. #1
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Do I Need To Re-organize My Program?

    "Non-static method xx() cannot be referenced from static context"
    There is code in a static method that is trying to call a non-static method. Non-static methods don't exist without an instance of the class.

    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.

  3. #3
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Do I Need To Re-organize My Program?

    A.amethod()
    If A is a classname, that is coded as if amethod() is a static method in A.
    To call a non-static method, you need a reference to the A class:
    aRefToA.amethod();


    a.amethod(). Is that a good alternative?
    Yes

    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.

  5. #5
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default 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

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Do I Need To Re-organize My Program?

    Quote Originally Posted by petec View Post
    I'll have to think about it overnight.
    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

  7. #7
    BinaryDigit09 is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default 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.

  8. #8
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default 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.

  9. #9
    BinaryDigit09 is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default 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?
    Yes it can. Sounds like you're starting to get a handle on it. And even though it's wierd, it makes perfect sense to do so. Consider this:

    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; }
    
    }
    The reference to 'a' is static on purpose. It means it belongs to the class A, not an instance of A. The benefit of making it static here is that anybody in the app can get ahold of 'a' by calling A a = A.getA() (on the class A). They will get the instance of 'a' that A created in its static main method. Once they have a reference to 'a', they can get ahold of 'b' and 'c' by calling B b = a.getB() or C c = a.getC() accordingly. Again they will get the correct references to the same objects that 'a' created in its constructor. Global access to all your objects without creating accidental copies.

  10. #10
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default 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

  11. #11
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default 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

  1. Replies: 8
    Last Post: 03-22-2014, 02:06 AM
  2. How to organize/design a date and time array?
    By RORE in forum New To Java
    Replies: 7
    Last Post: 07-05-2011, 10:44 AM
  3. Problem to organize my code in classes
    By ze snow in forum New To Java
    Replies: 4
    Last Post: 02-23-2010, 04:28 AM
  4. Organize class source code
    By Alejandro Valdez in forum Eclipse
    Replies: 0
    Last Post: 05-16-2008, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •