Results 1 to 15 of 15
- 02-08-2011, 09:59 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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.
-
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.
- 02-08-2011, 10:20 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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.
- 02-08-2011, 10:36 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
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!
- 02-08-2011, 10:44 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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?
-
- 02-08-2011, 11:10 PM #8
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Do I understand it right? You set:
Java Code:StatusWindow statusWindow = new StatusWindow(); statusWindow.append();
and it won't work?
- 02-08-2011, 11:31 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
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.
- 02-08-2011, 11:39 PM #10
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
re
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.
- 02-08-2011, 11:56 PM #11
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?
- 02-09-2011, 12:08 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
- 02-09-2011, 03:14 AM #13
Its quite simple really - based on my example, it might look something like this:
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).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); } }
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?
- 02-09-2011, 11:03 AM #14
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
thanks
Thanks for your example, I understand what you're saying now. I will post back to let you know how it's going.
- 02-09-2011, 01:51 PM #15
Similar Threads
-
Problem using packages.
By fgm1 in forum New To JavaReplies: 13Last Post: 11-05-2009, 07:41 PM -
Java Packages/Jars
By Silentstormz in forum Advanced JavaReplies: 4Last Post: 09-14-2008, 07:58 PM -
Importing packages from the packages within same application.
By sta2003 in forum New To JavaReplies: 3Last Post: 02-12-2008, 11:03 AM -
packages problem
By cew27 in forum New To JavaReplies: 5Last Post: 01-10-2008, 09:14 PM -
packages
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks