Diff between static and non static members
what is the difference between static and non static members with an example???.(in detail please)
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Thavaselvan PalanivelPosted Jun 30, 2015, 2:32 PM
Non Static: A non-static variable is specific to a single instance of that class.
* Static: There is only one copy of static variable and even
when the class is instantiated, the value remains the same.
Non Static: Every time the class is instantiated, the object has
their own copy of these variables.
Example:
public class Test {
static int a=6;
int b=8;
public static void main(String[] args) {
Test obj1=new Test();
Test obj2=new Test();
obj1.a=10;
obj2.a=12;
obj1.b=24;
obj2.b=36;
System.out.println("Static a"+obj1.a); //12
System.out.println("Static a"+obj2.a); //12
System.out.println("Static a"+a); //12
System.out.println("Static b"+obj1.b); //24
System.out.println("Static b"+obj2.b); //36
}
}
RakeshPosted Jun 30, 2015, 2:21 PM
Upendra Pratap ShahiPosted Jun 30, 2015, 8:23 AM
Rajeesh MenothPosted Jun 30, 2015, 8:00 AM