RocketMan on 29/4/2009 at 22:29
Hey all. I'm working on a rather large program that's pretty important to me. I'd like to finish it by mid summer but I have a rather noobish problem. Maybe it's just because I took a break from programming for a while and forgot some stuff I don't know but anyway here's the issue:
I'm compiling my code using the SDK from Sun Microsystems. It uses a dos command line. Right now I have about 14 errors or so and they're almost all related to instance method calls and instance variables.
My main class has a constructor in it which initializes many of my interface variables as well as setting up the interface itself. My main method instantiates the constructor as well as the constructors in a couple of other classes. My main class also implements the Runnable interface for a thread. Within the run() function for the thread I run my core loop, which calls various functions from different classes that do a whole slew of math operations. I also parse some objects defined in my main constructor. All of my errors so far say that the VM cannot find the variables I'm trying to parse because it can't find them. This is obviously an inheritance problem. For some reason I can't seem to get the VM to find stuff defined within the constructor when I call it from the run() method. Here's a hypothetical example of how my program is laid out:
class program implements Runnable {
global variable declarations
program() {
JComboBox example = new JComboBox();
Rest of constructor code....
}
public static void main(String args[]) {
program inst = new program();
class2 inst2 = new class2();
Thread runner = new Thread(this);
runner.start();
etc...
}
public void run() {
double number = Double.parseDouble(example.getString());
}
}
Ok this is a really dirty program i wrote out of my head which probably has errors and isn't anything like the real thing EXCEPT for the basic structure, which is what's important. I don't want to put the actual code because it's huge and confusing. Essentially the problem is that when i try to parse "example", the VM can't find it. Logic would tell me to use the instance "inst" to locate it via "inst.example" but "inst" is defined in a static context and when I tried it it didn't work either. I don't want to call the constructor again by making another instance within the run method because I think that would screw up the program by running the same code again which would be bad.
How do I access instance vars defined in my constructor when i'm doing it outside of the constructor in another method like run() ?? Thanks for the help.
dvrabel on 30/4/2009 at 00:34
You need to read a book on Java as you've not grasped some pretty important fundamentals.
Nameless Voice on 30/4/2009 at 00:56
Basically, it looks like you're declaring and initialising local variables inside the constructor. Those aren't a part of the class, they're local to the constructor method.
You need to put the variables into the private section of the class and then only assign values to them, instead of declaring them inside your constructor.
RocketMan on 30/4/2009 at 01:03
Quote Posted by dvrabel
You need to read a book on Java as you've not grasped some pretty important fundamentals.
I have, 3 times, I just get a little tied up in the hierarchical structure of the programming language unfortunately.
NV: Do you think I should make them static or is there a more robust way that doesn't clutter up the top of the code area? All of the vars in question are being utilized in the constructor and some are called elsewhere.
Nameless Voice on 30/4/2009 at 01:26
Static? Only if you want them to be shared across all instances of the class, or if you only plan to have one instance of the class. And they still need to go inside the class, not the constructor.
RocketMan on 30/4/2009 at 02:05
So am I correct in assuming that if you want to freely reference something from anywhere in the class then it can't be contained in the brace brackets of a method or a constructor? Then I could use the one instance of the class that I made to point to any given variable? I guess this means the vars have to be out in the open?
class program implements Runnable {
global variable declarations
JComboBox example;
program() {
example = new JComboBox();
Rest of constructor code....
}
public static void main(String args[]) {
program inst = new program();
class2 inst2 = new class2();
Thread runner = new Thread(this);
runner.start();
etc...
}
public void run() {
double number = Double.parseDouble(inst.example.getString());
}
}
Volca on 30/4/2009 at 05:58
The variables will always be visible in the scope defined by the first curly braces enclosing the declaration, and all the scopes included in that one (unless masked). Class member variables should be declared inside the class scope directly (preferably protected or private to avoid unwanted access from outside the class) - basically you have it right in the last code snippet but you *should* include the visibility keyword explicitly (i.e. private/protected keyword). The inst.example.getString() should really just be example.getString() - the run method sees all the variables in the scopes enclosing it.
Also note class names should begin with Upper case letter according to Java coding guides. It is useful because you'll be quickly able to tell package name from a class name.
just my 2 cents :)
Al_B on 30/4/2009 at 21:57
java is not my forte so I may be wrong on this. In the above code, I would expect the run() function to have difficulty in seeing the 'inst' variable as it was declared as local to another function - main().
You could probably substitute 'this' in place of 'inst' in run() - but as Volca says you should be able to just drop both as the variable should be visible to the run() function.
I suspect you'd probably benefit from a bit of a refresher on scoping rules. There are plenty of internet resources but making the best use of them will require experience as much as anything else.
RocketMan on 30/4/2009 at 22:13
I hear ya. Before posting I hopped around from site to site reading pretty much the same thing everywhere. I understand the general rules but when it comes to coding i find it can get a bit confusing without comparable examples. I've made large programs in the past for work involving half a dozen classes or more but I believe the reason I got away with it was because I was lazy and made every variable that needed to be updated static (well..except for local vars in methods and such). This is sort of a cop-out of you don't want to worry about scope. This time around I was actually trying to be a bit more tidy with my code but ran into trouble as you can see.
Al_B on 30/4/2009 at 22:23
Yeah - you want to avoid static variables unless you have no other choice. They've definitely got their uses but most of the time you want to isolate and contain functionality within instances of classes.
When you get some time, check out (
http://www.cc2e.com/) code complete. I suspect you'll benefit from reading at a higher level than a straightforward language reference. Most of the time language books spend a huge amount of time on syntax which is not what you probably need at the moment. The book covers FAR more than your specific issues but if you haven't read it then it's worth considering.