Results 1 to 15 of 15
  1. #1
    brendan_1986 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Java packages / statsic problem - Please help!

    Hello,

    I am writing a chat client. It has about 10 .java files so far. The problem is how to access variables, and such without having to turn everything static, which people keep saying is bad.

    I have a MainClass.java file, a ChatConnection.java file, and a StatusWindow.class. How would I access the variables on StatusWindow.java from anoter file without having to put the static keyword in front of it. I do this:

    In the ParentFrame (MDI) I have: statusWindow instance. I then need to access the statusWindow.append() method, but without making it static. I hope that makes sense. I have tried using a package, that is putting package name; before each file and then using import name.*; But I still cannot access any of those variables without making the statusWindow variables static.
    Last edited by brendan_1986; 02-08-2011 at 10:03 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way to solve this would be to pass references of one object to another. So if a Foo object needs to call the append method of your StatusWindow then pass the StatusWindow object into the Foo object via a method such as setStatusWindow(StatusWindow myStatusWindow) and set a field with the object passed in via the parameter.

  3. #3
    brendan_1986 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    But surely when I put package name; in front of every file, set the classpath and then do import name.*;, I'd have access to all those variables? How do you property access methods of a class without making everything static. It's difficult. Or is it okay to change a few of the required vars to static?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by brendan_1986 View Post
    But surely when I put package name; in front of every file, set the classpath and then do import name.*;, I'd have access to all those variables? How do you property access methods of a class without making everything static. It's difficult. Or is it okay to change a few of the required vars to static?
    Clearly you don't as the need for many static variables is a flag for a broken design and a likely need to re-design the application. Your goal is to have each object managing its own behaviors as much as possible and communicating only essential information or messages to other objects that are concerned with their own behaviors and data. Two fundamental concepts of object oriented programming including encapsulation -- avoiding the unnecessary exposure of data, and decoupling -- the separation of groups of data and behaviors into their own units, as isolated from others as possible.

  5. #5
    NeuroFuzzy is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    The correct way of doing this would be passing instances of those classes in the constructor, and adding get() methods.

    If you have multiple instances of one object, making the objects static isn't even an option.

    I don't think you have your head straight about what "static" means. You don't have access to all the variables if you've imported the file because you haven't created an instance of the class yet. Java is an Object Oriented language!

  6. #6
    brendan_1986 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Okay, thanks for the quick replies.Here is my question, a bit simplfied.

    I have a class called ParentFrame declared in this class is StatusWindow statusWindow. How do I access the append method of the statusWindow instance, from a different file. If I do: statusWindow.append() it says cannot find variable statusWindow, How can I access that casas instance?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by brendan_1986 View Post
    Okay, thanks for the quick replies.Here is my question, a bit simplfied.

    I have a class called ParentFrame declared in this class is StatusWindow statusWindow. How do I access the append method of the statusWindow instance, from a different file. If I do: statusWindow.append() it says cannot find variable statusWindow, How can I access that casas instance?
    Again to repeat what I said before and what neurofuzzy is saying, one way is to pass a reference of the StatusWindow object into the class that needs to call the method.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default

    Do I understand it right? You set:
    Java Code:
    StatusWindow statusWindow = new StatusWindow();
    statusWindow.append();

    and it won't work?

  9. #9
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    do some research on the mvc pattern

    1) controller holds the listeners
    2) when user does something a listener is called in the controller
    3) controller calls a method in model to change its data
    4) model notifies the view that it changed
    5) view updates it self

    i had a badly coded program with statics everywhere i changed it to a nice OO program this way. Thats the point where you learn to design before coding. If you don't have an initial plan you will probably create ugly code which at some stage will cause bugs & headaches & waste of time on debugging.

  10. #10
    brendan_1986 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default re

    Quote Originally Posted by milovan View Post
    Do I understand it right? You set:
    Java Code:
    StatusWindow statusWindow = new StatusWindow();
    statusWindow.append();

    and it won't work?
    statusWindow is in the ParentFrame class. I am trying to call append outside of the classit's decalred in. If I do ParentFrame.statusWindow.append() it requires it to be a static declaration. If I was calling append in the above example, it would work but I need to access it outside of the class it's declared in.

    Java Code:
    // ParentFrame.java
    StatusWindow window;
    ...

    Java Code:
    // Connection.java
    window.append("connecting .."); // 'cannot find symbol window'..
    Last edited by brendan_1986; 02-08-2011 at 11:41 PM.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    So you have some class A and inside you have some class B. You want to access B from some class C? Is that correct?

    If thats the case, you can add an accessor method to A. It can either return B directly, or it can forward information to and from B without show B to C directly. Make sense?

  12. #12
    brendan_1986 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    So you have some class A and inside you have some class B. You want to access B from some class C? Is that correct?

    If thats the case, you can add an accessor method to A. It can either return B directly, or it can forward information to and from B without show B to C directly. Make sense?
    thanks. Yes that is exactly what am I am doing. I will elarn what accessor methods are. thank all.

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Its quite simple really - based on my example, it might look something like this:
    Java Code:
    public class A{
        private B b;
    
        public A(){
            b = new B();
        }
    
        public B getB(){
            return b;
        }
    
        class B{
            int number;
            public int getNumber(){
                return number;
            }
    
            public void setNumber(int number){
                this.number = number;
            }
        }
    }
    
    
    public class C{
        private A a;
    
        public C(){
            a = new A();
            //Get access to b
            B b = a.getB();
            //get a value from b
            int x = b.getNumber();
            //Access something from B anonymously
            a.getB().setNumber(55);
        }
    }
    In the above example, A contains the definition for B. A also contains an instance of B. the method in A called getB() returns a reference to B. Outside classes that have access to A can get access to B through this method. It is an accessor method (a getter).

    B also contains accessors - setNumber() (a setter), and getNumber() (a getter). Any method that allows you to set something or get something is an accessor by definition. Does this code help make it clear?

  14. #14
    brendan_1986 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default thanks

    Thanks for your example, I understand what you're saying now. I will post back to let you know how it's going.

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

Similar Threads

  1. Problem using packages.
    By fgm1 in forum New To Java
    Replies: 13
    Last Post: 11-05-2009, 07:41 PM
  2. Java Packages/Jars
    By Silentstormz in forum Advanced Java
    Replies: 4
    Last Post: 09-14-2008, 07:58 PM
  3. Replies: 3
    Last Post: 02-12-2008, 11:03 AM
  4. packages problem
    By cew27 in forum New To Java
    Replies: 5
    Last Post: 01-10-2008, 09:14 PM
  5. packages
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 12:10 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
  •