What is Non Static Variables?
- ·
Non – static variables are object members.
- ·
Non – static variables are created
inside class outside method without using static keyword.
- ·
These variables are also called as
instance variable.
- ·
Only after creating object, we can
access non-static variable.
- ·
It is not mandatory to initialize .
If we donot initialize , depending upon data type default value will store into
it.
public class B{
int x=10;
public static void main(String [] args){
System.out.println(x);
}
}
Correct Program
public class B{
int x=10;
public static void main(String []args){
B b1 = new B();
System.out.println(b1.x);
}
}
Non static main original value will not change only
xerox copy will change as shown in below example
public class A{
int x=10;
public static void main(String [] args){
A a1= new A();
a1.x=1000;
System.out.println(a1.x);
A a2 = new A();
System.out.println(a2.x);
}
}
No comments