• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's this about static variables?  RSS feed

 
David Scott
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a small java application for a research project (code included below). I'm trying to use an object (e.g., Observer) and Eclipse is giving me this error: Cannot make a static reference to the non-static field observerA (line 21 of the code, I'll point it out in the code because I don't think copy-and-paste will carry over the line numbers). Eclipse has been great in helping me code, but I think I'd have to be a programmer to understand the warning/error messages.

The idea of this program is simple: There are two observers on a line (just an x coordinate for location). They try and move towards each other with a gaussian step modified by a mean and a variance. Each trial they each take one step towards each other. I want the observers to exist for all the trials, but new observers should be created when a new mean or new variance is to be tested. That's where I seem to be having the problem.



I'm reading through Head First Java on Safari Bookshelf (access is free from campus) in the meantime so that I can learn Java, but I'll need to get this project done without knowing it all so any direct help will save me a lot of effort.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a situation like this, where the main method of StatsChurner represents only a process, and is not being used in an object-oriented way, it's best just to define all variables inside the method. That way it's clear that the scope of the vaiables is the method, and there is no confusion. So, if your goal is just to get through writing this one program, just move the variable definitions inside the main method and stop reading here.

The way you're currently doing it, you've defined observerA and observerB as instance variables. That means they only have meaning when you create an instance of the StatsChurner class. Since your main method is a static method, it can't reference instance variables unless you create an instance of the class. For example, the following would be allowed:

Another possible way to make this work would be to define the variables outside the main method and add the decriptor "static" to them. This descriptor makes it so there's only one copy of the variable shared by all instances of the class (sometimes called a class variable). It also makes it so the variable can be referenced without even having an instance of the class. It then becomes ok to write

inside your static main method.
 
David Scott
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

Putting everything in main did solve my problem and allow me to finish the program.

Also, thank you for the extra explanation. Now I get what Eclipse kept trying to tell me with those tool tips. ^_^ I am interested in learning object oriented programming so it was good to get some kind of understanding as to what is going on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!