Monday, September 14, 2020

Static variable

package code;

public class StaticVar {
static int a =10;
int b =20;

public static void main(String[] args) {

StaticVar ob =new StaticVar();
ob.a=100;


System.out.println(ob.b=200);   //200
System.out.println(StaticVar.a); //100

StaticVar ob2 = new StaticVar();
System.out.println(ob2.a); //100
System.out.println(ob2.b); //20

//jb bhi new object create krengy instance variable new memory allocate krega, aur previous value loose krke instance variable ki default value hold krega.

but static variable static block me memory allocate krta hai and share with different different objects, iski value again intialize nhi hogi.

But instance variable hr ek object k sath memory allocate krega.



}

}

No comments:

Post a Comment

Guitar Chords

click on   Guitar Chords Family