Valhalla Legends Forums Archive | Java Programming | Inheritance question

AuthorMessageTime
MoNksBaNe_Agahnim
Not sure if this deal with inheritance but here it goes. I am using netbeans and making an applet. I have two forms and I want to use the variables declared in form1 in form2. How would I got about this?
October 12, 2004, 3:21 AM
Myndfyr
You either need to make them available as either public members or through public getter and setter functions.  Both are bad practice, IMHO, and it's better to encapsulate these kinds of multi-UI-element-variables elsewhere.  For example, with settings interfaces in my bot, I have shared settings encapsulated in a single class with a ton of variables.  Then the six or seven client classes that need access to these variables just get a reference to that object.
October 12, 2004, 5:14 AM
The-FooL
Getters/Setters are probably best.  Another option, depending on what your actually doing, is just to pass the variables through method or constructor calls.  Also, instead of multiple references to a settings class, you can make a settings class static and get them without a reference.
October 12, 2004, 11:24 PM
MoNksBaNe_Agahnim
could I get some examples. I am such a noob and before I stopped programming I didn't get into encapsulation. Appreciate the help guys!
October 13, 2004, 3:17 AM
Myndfyr
[quote author=The-FooL link=topic=9107.msg84135#msg84135 date=1097623442]
Getters/Setters are probably best.  Another option, depending on what your actually doing, is just to pass the variables through method or constructor calls.  Also, instead of multiple references to a settings class, you can make a settings class static and get them without a reference.
[/quote]

In my case, it wasn't an option to make settings static, because I wanted to be able to have multiple connection instances using multiple settings sets.  But you are indeed correct; if you have only one use for them, then static members would work just as well (but remember, you still ought to encapsulate them with static getters/setters!)
October 13, 2004, 3:53 AM
CrAzY
public double num1 = 1.0;

Get the concept?
October 19, 2004, 12:16 AM

Search