Static Variable:
Let me give you an example....
class Student{
int sid;
}
Suppose... if you create 2 objects or instances for the student...
Student one = new Student();
Student two = new Student();
Student object one will have the sid
and Student object two will have its own sid..
So if you change the sid of one..,sid in two won't be changed... because
they both are different.........
Static variable:
----------------
class Student{
static int counter;
}
Now assume you created... 3 student objects
Student one = new Student();
Student two = new Student();
Student three = new Student();
As counter is declared as... static... all 3 student objects refer to same... counter variable...
Static means per class.... so... all the objects or instances of that class will have only counter
which is unique per class...
Even if you create 10000 student objects there will be only one counter variable.... and all
10000 students have access to.. that counter variable... if any student object changes the counter
variable that change will be reflected to alll the objects........
Example of non static and static variables...
--------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Rishitha
*/
public class StaticEx {
static int counter;
int nonstatic;
public static void main(String args[]){
//non static variable
StaticEx one = new StaticEx();
one.counter = 10;
one.nonstatic = 20;
StaticEx two = new StaticEx();
System.out.println(two.counter);//output is 10...
//because... there is only one copy of counter per
//class...
System.out.println(two.nonstatic);//output is 0
//because... I have assigned 20 to only one object
//as I didn't assign any value to nonstatic variable
//its default value is printed which is zero...
//To access... static variable... you need not object...
//In the previous example... i have used.. one.counter...
//but you can use counter by writing
//Classname.counter also...
//That means... StaticEx.counter
System.out.println(StaticEx.counter);
}
}
Let me give you an example....
class Student{
int sid;
}
Suppose... if you create 2 objects or instances for the student...
Student one = new Student();
Student two = new Student();
Student object one will have the sid
and Student object two will have its own sid..
So if you change the sid of one..,sid in two won't be changed... because
they both are different.........
Static variable:
----------------
class Student{
static int counter;
}
Now assume you created... 3 student objects
Student one = new Student();
Student two = new Student();
Student three = new Student();
As counter is declared as... static... all 3 student objects refer to same... counter variable...
Static means per class.... so... all the objects or instances of that class will have only counter
which is unique per class...
Even if you create 10000 student objects there will be only one counter variable.... and all
10000 students have access to.. that counter variable... if any student object changes the counter
variable that change will be reflected to alll the objects........
Example of non static and static variables...
--------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Rishitha
*/
public class StaticEx {
static int counter;
int nonstatic;
public static void main(String args[]){
//non static variable
StaticEx one = new StaticEx();
one.counter = 10;
one.nonstatic = 20;
StaticEx two = new StaticEx();
System.out.println(two.counter);//output is 10...
//because... there is only one copy of counter per
//class...
System.out.println(two.nonstatic);//output is 0
//because... I have assigned 20 to only one object
//as I didn't assign any value to nonstatic variable
//its default value is printed which is zero...
//To access... static variable... you need not object...
//In the previous example... i have used.. one.counter...
//but you can use counter by writing
//Classname.counter also...
//That means... StaticEx.counter
System.out.println(StaticEx.counter);
}
}
No comments:
Post a Comment