I've written this test code:
class MyProgram
{
int count = 0;
public static void main(String[] args)
{
System.out.println(count);
}
}
But it gives the following error:
Main.java:6: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^
How do I get my methods to recognize my class variables?
It is ClassLoader responsible to load the class files.Let's see what happens when we write our own classes.
Example 1:
Now we can see that class "StaticTest" has 3 fields.But actually there is no existence of b,c member variable.But why ???. OK Lest's see. Here b,c are instance variable.Since instance variable gets the memory at the time of object creation. So here b,c are not getting any memory yet. That's why there is no existence of b,c. So There is only existence of a. For ClassLoader it has only one information about a. ClassLoader yet not recognize b,c because it's object not instantiated yet.
Let's see another example: Example 2:
Now if we try to compile this code compiler will give CE error. CE: non-static method display() cannot be referenced from a static context.
Now For ClassLoader it looks like:
In Example 2 CE error is because we call non static method from a static context. So it is not possible for ClassLoader to recognize method display() at compile time.So compile time error is occurred.
The very basic thing is static variables or static methods are at class level. Class level variables or methods gets loaded prior to instance level methods or variables.And obviously the thing which is not loaded can not be used. So java compiler not letting the things to be handled at run time resolves at compile time. That's why it is giving you error non-static things can not be referred from static context. You just need to read about Class Level Scope, Instance Level Scope and Local Scope.
Now you can add/use instances with in the method
Let's analyze your program first.. In your program, your first method is
main()
, and keep it in mind it is the static method... Then you declare the local variable for that method (compareCount, low, high, etc..). The scope of this variable is only the declared method, regardless of it being a static or non static method. So you can't use those variables outside that method. This is the basic error u made.Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing. *Static method calls only the static method and use only the static variable. *Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class) *Because of this you call it as a class variable or a class method. And a lot more is there about the "static" keyword. I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).
And the advice for you is: you misunderstood the idea of the scope of the variables and static functionalities. Get clear idea about that.
The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.
Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class
As a corollary to the above
The lifetime of a static field/method is equivalent to the lifetime of your application
E.g. Say, car has the property colour, and exhibits the behaviour 'motion'. An instance of the car would be a Red Volkswagen Beetle in motion at 25kmph.
Now a static property of the car would be the number of wheels (4) on the road, and this would apply to all cars.
HTH
The
static
keyword modifies the lifecycle of a method or variable within a class. Astatic
method or variable is created at the time a class is loaded. A method or variable that is not declared asstatic
is created only when the class is instantiated as an object for example by using thenew
operator.The lifecycle of a class, in broad terms, is:
new
operator using the class to make an instance of the class as an actual object and then when done with the objectIn order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called
main()
. Since the method must exist whether the class containing the main method has been instantiated or not, themain()
method must be declared with thestatic
modifier so that as soon as the class is loaded, themain()
method is available.The result is that when you start your Java application by a command line such as
java helloworld
a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in thehelloworld
class that is calledmain(String [] args)
. this method must bestatic
so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at themain()
method.So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the
static
modifier. Once your Java program has started with themain()
function you can then use any variables or methods that have the modifier ofstatic
since they exist as part of the class being loaded.However, those variables and methods of the class which are outside of the
main()
method which do not have thestatic
modifier can not be used until an instance of the class has been created as an object within themain()
method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have thestatic
modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.