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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajeesh MenothRank #118 · 18.4K reputationPosted Jun 30, 2015, 4:00 AM EDT
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Jun 30, 2015, 4:23 AM EDT
RakeshRank #397 · 6.3K reputationPosted Jun 30, 2015, 10:21 AM EDT
Thavaselvan PalanivelRank #2,151 · 661 reputationPosted Jun 30, 2015, 10:32 AM EDT
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
}
}