static keyword:
The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods and blocks. The static keyword belongs to the class than instance of the class. The static can be:
>1. variable (also known as class variable)
>2. method (also known as class method)
>3. block
>1. static variable:
If you declare any variable as static, it is known static variable.
>The static variable can be used to refer the common property of all objects
(that is not unique for each object) e.g. company name of employees,college name of students etc.
>The static variable gets memory only once in class area at the time of class.
Advantage of static variable:
It makes your program memory efficient (i.e it saves memory).
Understanding problem without static variable:
class Employee{
int eid;
String name;
String company="TCS";
}
Suppose there are 200 employees in company, now all instance data members will get memory each time.
When object is created.All employee have its unique eid and name so instance data member is good.Here,company refers to the common property of all objects.If we make it static,this field will get memory only once.
Note:static field is shared by all objects.
Example of static variable:
class Employee{
int eid;
String name;
static String company ="TCS";
Employee(int r, String n){
rollno = r;
name = n;
}
void show () { System.out.println(eid+" "+name+" "+company); }
public static void main(String []args){
Employee s1 = new Employee(123,"vishwa");
Employee s2 = new Employee(456,"dolly");
s1.show ();
s2.show ();
}
}
Output: 123 vishwa TCS
456 dolly TCS
Below there is an example of static variables to see the difference.
Program of counter without static variable:
In this example, we have created an instance variable named count which is incremented in the constructor.Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.
class Counter{
int count=0; //will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String []args){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output: 1
1
1
Program of counter by static variable:
class Counter{
static int count=0; //will get memory only once and retain its value
Counter(){
count++;
System.out.println(count);
}
public static void main(String []args){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output: 1
2
3
Note:As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
>2. static method:
If you apply static keyword with any method, it is known as static method.
>A static method belongs to the class rather than object of a class.
>A static method can be invoked without the need for creating an instance of a class.
>static method can access static data member and can change the value of it.
Example of static method:
class Employee{
int eid;
String name;
static String company = "TCS";
static void change(){
company = "Infosys";
}
Student(int i, String n){
eid = i;
name = n;
}
void show () { System.out.println(eid+" "+name+" "+company);}
public static void main(String []args){
Employee.change();
Employee s1 = new Employee(123,"vishwa");
Employee s2 = new Employee(456,"karan");
Employee s3 = new Employee(789,"dolly");
s1.show();
s2.show();
s3.show();
}
}
Output: 123 vishwa Infosys
456 karan Infosys
789 dolly Infosys
Restrictions for static method:
There are two main restrictions for the static method. They are:
>1.The static method can not use non static data member or
call non-static method directly.
>2.this and super cannot be used in static context.
Program of accessing non-static data member directly from static method main:
class Test{
int a=40; //non static
public static void main(String []args){
System.out.println(a);
}
}
Output: Compile Time Error
Que)why main method is static?
Ans) Because object is not required to call static method if it will be non-static method, jvm creats object first then call main() method that will lead the problem of extra memory allocation and it will take more time also.
>3. static block:
>Is used to initialize the static data member.
>It is excuted before main method at the time of classloading.
Example of static block:
class Test{
static { System.out.println("static block is invoked ");}
public static void main(String []args){
System.out.println("Hello Main");
}
}
Output: static block is invoked
Hello Main
Que)Can we execute a program without main() method?
Ans) Yes, one of the way is static block but in previous
version of JDK not after JDK 1.7.
Program of executing a program without main() method:
class Test{
static{
System.out.println("static block is invoked");
}
}
Output: static block is invoked (if not JDK7)
Note:Because from JDK7 static block is discard
The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods and blocks. The static keyword belongs to the class than instance of the class. The static can be:
>1. variable (also known as class variable)
>2. method (also known as class method)
>3. block
>1. static variable:
If you declare any variable as static, it is known static variable.
>The static variable can be used to refer the common property of all objects
(that is not unique for each object) e.g. company name of employees,college name of students etc.
>The static variable gets memory only once in class area at the time of class.
Advantage of static variable:
It makes your program memory efficient (i.e it saves memory).
Understanding problem without static variable:
class Employee{
int eid;
String name;
String company="TCS";
}
Suppose there are 200 employees in company, now all instance data members will get memory each time.
When object is created.All employee have its unique eid and name so instance data member is good.Here,company refers to the common property of all objects.If we make it static,this field will get memory only once.
Note:static field is shared by all objects.
Example of static variable:
class Employee{
int eid;
String name;
static String company ="TCS";
Employee(int r, String n){
rollno = r;
name = n;
}
void show () { System.out.println(eid+" "+name+" "+company); }
public static void main(String []args){
Employee s1 = new Employee(123,"vishwa");
Employee s2 = new Employee(456,"dolly");
s1.show ();
s2.show ();
}
}
Output: 123 vishwa TCS
456 dolly TCS
Below there is an example of static variables to see the difference.
Program of counter without static variable:
In this example, we have created an instance variable named count which is incremented in the constructor.Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.
class Counter{
int count=0; //will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String []args){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output: 1
1
1
Program of counter by static variable:
class Counter{
static int count=0; //will get memory only once and retain its value
Counter(){
count++;
System.out.println(count);
}
public static void main(String []args){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output: 1
2
3
Note:As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
>2. static method:
If you apply static keyword with any method, it is known as static method.
>A static method belongs to the class rather than object of a class.
>A static method can be invoked without the need for creating an instance of a class.
>static method can access static data member and can change the value of it.
Example of static method:
class Employee{
int eid;
String name;
static String company = "TCS";
static void change(){
company = "Infosys";
}
Student(int i, String n){
eid = i;
name = n;
}
void show () { System.out.println(eid+" "+name+" "+company);}
public static void main(String []args){
Employee.change();
Employee s1 = new Employee(123,"vishwa");
Employee s2 = new Employee(456,"karan");
Employee s3 = new Employee(789,"dolly");
s1.show();
s2.show();
s3.show();
}
}
Output: 123 vishwa Infosys
456 karan Infosys
789 dolly Infosys
Restrictions for static method:
There are two main restrictions for the static method. They are:
>1.The static method can not use non static data member or
call non-static method directly.
>2.this and super cannot be used in static context.
Program of accessing non-static data member directly from static method main:
class Test{
int a=40; //non static
public static void main(String []args){
System.out.println(a);
}
}
Output: Compile Time Error
Que)why main method is static?
Ans) Because object is not required to call static method if it will be non-static method, jvm creats object first then call main() method that will lead the problem of extra memory allocation and it will take more time also.
>3. static block:
>Is used to initialize the static data member.
>It is excuted before main method at the time of classloading.
Example of static block:
class Test{
static { System.out.println("static block is invoked ");}
public static void main(String []args){
System.out.println("Hello Main");
}
}
Output: static block is invoked
Hello Main
Que)Can we execute a program without main() method?
Ans) Yes, one of the way is static block but in previous
version of JDK not after JDK 1.7.
Program of executing a program without main() method:
class Test{
static{
System.out.println("static block is invoked");
}
}
Output: static block is invoked (if not JDK7)
Note:Because from JDK7 static block is discard
No comments:
Post a Comment