File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Non-static variable cannot be referenced from static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non-static variable cannot be referenced from static context" Watch "Non-static variable cannot be referenced from static context" New topic
Author

Non-static variable cannot be referenced from static context

Amil Mehmedagic
Greenhorn

Joined: Feb 27, 2004
Posts: 22
I am getting the error message non-static variable clockwise cannot be referenced from a static context, when running the program code below. How do I counter this problem?
The program below asks a user to input a probability and number of circles.
The program then animates the input number of circles around a square of side 400 pixels. At each move there is a probability that each circle moves anticlockwise. If it does so the variable count counts that circle.
When all circles are moving anticlockwise, the animation needs to stop.
Below is the code of the two classes. The error occurs in the main method of the Main class. This is where I check whether a circle moves anticlockwise to assign it to the count variable.
Please help in solving the above problem! [ edited to break long lines and remove the evil tab character -ds ]
[ April 27, 2004: Message edited by: Dirk Schreckmann ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
For one thing, should be
Since clockwise is an instance variable.
IMHO, your convention of naming a reference to an object the lower case of the object class name is pretty error-prone.


Mike Gershman
SCJP 1.4, SCWCD in process
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
More correctly it should be

else you have an assignment instead of a comparison.
Which doesn't solve the problem that you're calling instance variables from static code, something that's simply impossible.
Either make sure you have an instance to call (create one...) or make the variable static (if possible).
Any book or tutorial should be able to help you out in this, it's so basic.
Repeat: you cannot use instance variables from static code (after all, the static code doesn't belong to any instance so how can it know about the value of an instance variable???).


42
Tobias Huebner
Greenhorn

Joined: Apr 26, 2004
Posts: 1
The problem is that you don't have a instance of the Class Circle called "Circle" !!!
But you want to call on "Circle". Remember java is case-sensetive.

All you have is an array of the type Circle called "circle".

You should instanciate the class Circle and use the clockwise variable like this:

But in your case I think you might want to use the array of Circle you already have:

since you are in a loop.
I hope i was of some help for you. This was my first posting.
So good luck :0)
[ April 26, 2004: Message edited by: Tobias Huebner ]
[ April 27, 2004: Message edited by: Tobias Huebner ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non-static variable cannot be referenced from static context