• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Non-static variable cannot be referenced from static context

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???).
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic