static variable
static variable belongs to the class and not to object/instance
static variables are initialized only once , at the start of the execution .
A single copy to be shared by all instances of the class
A static variable can be accessed directly by the class name and there is no need of object
variable declared as static are, essentially, global variable.
When object of its class are declared, no copy of a static variable is made.
Instead, all instances of the class share the same static variable.
Syntax of static variable : public static int test;
static Method
static Method belongs to the class and not to the object/instance.
A static method can access only static variable. It can not access non-static variable
A static method can call only other static methods and can not call a non-static method from it.
A static method can be accessed directly by the class name and there is no need of object
Syntax :public static void total()
A static method cannot refer to "this" or "super" keywords in anyway
Example of static variable
public class StaticClass {
public static int test;
public void print1(){
System.out.println("print1: "+test);
}
public void print2(){
System.out.println("print2: "+test);
}
public static void total(){
System.out.println("total: "+10+test);
}
}
we declared test as static variable and a static method in StaticClass
public class democlass {
public static void main(String[] args) {
StaticClass.test=4;
StaticClass a1=new StaticClass();
a1.print1();
StaticClass b1=new StaticClass();
b1.print2();
//call static method
StaticClass.total();
}
}
print2: 4
total: 104
static variable belongs to the class and not to object/instance
static variables are initialized only once , at the start of the execution .
A single copy to be shared by all instances of the class
A static variable can be accessed directly by the class name and there is no need of object
variable declared as static are, essentially, global variable.
When object of its class are declared, no copy of a static variable is made.
Instead, all instances of the class share the same static variable.
Syntax of static variable : public static int test;
static Method
static Method belongs to the class and not to the object/instance.
A static method can access only static variable. It can not access non-static variable
A static method can call only other static methods and can not call a non-static method from it.
A static method can be accessed directly by the class name and there is no need of object
Syntax :public static void total()
A static method cannot refer to "this" or "super" keywords in anyway
Example of static variable
public class StaticClass {
public static int test;
public void print1(){
System.out.println("print1: "+test);
}
public void print2(){
System.out.println("print2: "+test);
}
public static void total(){
System.out.println("total: "+10+test);
}
}
we declared test as static variable and a static method in StaticClass
public class democlass {
public static void main(String[] args) {
StaticClass.test=4;
StaticClass a1=new StaticClass();
a1.print1();
StaticClass b1=new StaticClass();
b1.print2();
//call static method
StaticClass.total();
}
}
output
print1: 4print2: 4
total: 104
No comments:
Post a Comment