WHAT DOES THE KEYWORD 'STATIC' MEAN???
In simple terms, a static variable/method can be accessed without the use of an object. There'll be only one copy of static variable/method for all the objects. This facility will be useful in several situations. For example, a static variable say count in library management program can count the no. of users visited on each day.
POINTS TO BE REMEMBERED:
1) Whenever a class is loaded, its static statements are run first.
EG:
class call
{
int non_static=10;
static int var3=5,var4;
static //STATIC BLOCK WILL RUN FIRST
{
System.out.println("Inside static block");
var4=25;
System.out.println("var3="+var3+"\t"+"var4="+var4);
}
void print()
{
System.out.println(non_static);
}
public static void main(String a[])
{
call obj=new call();
obj.print();
}
}
OUTPUT:
Inside static block
var3=5 var4=25
10
2)Only one copy of static variable/method will be available in a class, which all the objects use.
3)Static methods can call only other static methods.
4)Static methods must access only static data. This means that, nonstatic variables which are already declared outside the static method can't be accessed inside static method. But it is possible to declare nonstatic variable inside static methods and use it.
5)They can't refer to 'this' or 'super' keyword. (Since these keywords are related to objects).
EG:
public class Static
{
int non_static=10;
static int var1=5,var2;
void nonstatic_method()
{
System.out.println("this is nonstatic_method");
System.out.println("var1="+var1);
//in nonstatic methods, both static and nonstatic variables can be used.
}
static void static_method()
{
System.out.println("this is static_method");
System.out.println("non_static variable="+non_static);
//ERROR! since this method is static, it can't use non static variables
System.out.println("var1="+var1);
System.out.println("var2="+var2);
nonstatic_method();
//This is again an error since static method can call only other static methods
}
public static void main(String a[])
{
Static obj=new Static();
obj.nonstatic_method();
//Object is mandatory to call nonstatic methods
static_method();
//static methods can be called without using object.
}
}
OUTPUT:
this is nonstatic_method
var1=5
this is static_method
var1=5
var2=0
6)In the above example, static members are accessed from within the class in which they're declared. But when static members are to be accessed from another class, it must be done using the class name to specify which class' static member is accessed. For example, consider a class test which needs to access static variable var1 or static_method() of the previous eg.,
class test
{
public static void main(String a[])
{
Static.var1=6; //CLASS NAME IS MANDATORY HERE
Static.static_method();
}
}
OUTPUT:
this is static_method
var1=6
var2=0
7)And the last point is, note that we write as
public static void main(String str[])
and not as
public void main(String str[])
The reason behind is that, main() function is the starting point of a program. In java, main() is a part of a class and hence an object is required to access it. But when the program starts, JVM needs to call main() for which objects are not available. This is the reason we add static which indicates that main() function can be called even without an object.
Thanks for reading my blog... :-) Hope it is useful... Put your response in the comment... ;-)
No comments:
Post a Comment