Here we explain static keyword in term of varriable, method, class.
Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.
Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final.
However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.
e.g.
public class StaticVariable{
static int noOfInstances;
StaticVariable (){
noOfInstances++;
}
publ ic static void main(String[] args){
StaticVariable sv1 = new StaticVariable();
System.out. println("No. of instances for sv1 : " + sv1.noOfInstances);
StaticV ariable sv2 = new StaticVariable();
System.out. println("No. of instances for sv1 : " + sv1.noOfInstances);
System.ou t.println("No. of instances for st2 : " + sv2.noOfInstances);
StaticV ariable sv3 = new StaticVariable();
System.out. println("No. of instances for sv1 : " + sv1.noOfInstances);
System.ou t.println("No. of instances for sv2 : " + sv2.noOfInstances);
System.ou t.println("No. of instances for sv3 : " + sv3.noOfInstances);
}
}
S tatic methods can't use any instance variables. The this keyword can't be used in a static methods. You can find it difficult to understand when to use a static method and when not to use. If you have a better understanding of the instance methods and static methods then you can know where to use instance method and static method.
A static method can be accessed without creating an instance of the class. If you try to use a non-static method and variable defined in this class then the compiler will say that non-static variable or method cannot be referenced from a static context. Static method can call only other static methods and static variables defined in the class.
The concept of static method will get more clear after this program. First of all create a class HowToAccessStaticMethod. Now define two variables in it, one is instance variable and other is class variable. Make one static method named staticMethod() and second named as nonStaticMethod(). Now try to call both the method without constructing a object of the class. You will find that only static method can be called this way.
For Example :
public class HowToAccessStaticMethod{
int i;
static int j;
public static void staticMethod(){
System.out.pr intln("you can access a static method this way");
}
public void nonStaticMethod(){
i=100;
j= 1000;
System.out.println("Don 't try to access a non static method");
}
public static void main(String[] args) {
//i=100;
j=1000;
//nonS taticMethod();
staticMethod() ;
}
}
In order to understand the use of the static keyword in class declaration, we need to understand the class declaration itself. You can declare two kinds of classes: top-level classes and inner classes.
Answered by
Ni ..
, an ibibo Master,
at
8:38 PM on July 13, 2008