Skip to main content

Static variables are used when only one copy of it is required. Let me explain this with an example:-

class circle  {   public float _PI =3.14F;   public int Radius;      public funtionArea(int radius)   {   return this.radius * this._PI   }   }  class program  {   public static void main()   {   Circle c1 = new Cirle();   float area1 = c1.functionRaduis(5);   Circle c2 = new Cirle();   float area2 = c1.functionRaduis(6);  }   } 

Now here we have created 2 instances for our classclass circle , i.e 2 sets of copies of _PI_PI along with other variables are created. So say if we have lots of instances of this class multiple copies of _PI_PI will be created occupying memory.So So in such cases it is better to make such variables like _PI static_PI static and operate on them.

 class circle  {   static float _PI =3.14F;   public int Radius;     public funtionArea(int radius)   {   return this.radius * Circle._PI   }  }  class program  {   public static void main()   {   Circle c1 = new Cirle();   float area1 = c1.functionRaduis(5);      Circle c2 = new Cirle();   float area2 = c1.functionRaduis(6);     }   } 

Now no matter how many instances are made for the classclass circle , only one copy exists of variable _PI_PI saving our memory.

Static variables are used when only one copy of it is required. Let me explain this with an example:-

class circle  {   public float _PI =3.14F;   public int Radius;      public funtionArea(int radius)   {   return this.radius * this._PI   }   }  class program  {   public static void main()   { Circle c1 = new Cirle(); float area1 = c1.functionRaduis(5);   Circle c2 = new Cirle(); float area2 = c1.functionRaduis(6);  }   } 

Now here we have created 2 instances for our class circle , i.e 2 sets of copies of _PI along with other variables are created. So say if we have lots of instances of this class multiple copies of _PI will be created occupying memory.So in such cases it is better to make such variables like _PI static and operate on them.

 class circle  {   static float _PI =3.14F;   public int Radius;     public funtionArea(int radius)   {   return this.radius * Circle._PI   }  }  class program  {   public static void main()   { Circle c1 = new Cirle(); float area1 = c1.functionRaduis(5);      Circle c2 = new Cirle(); float area2 = c1.functionRaduis(6);     }   } 

Now no matter how many instances are made for the class circle , only one copy exists of variable _PI saving our memory.

Static variables are used when only one copy of it is required. Let me explain this with an example:

class circle { public float _PI =3.14F; public int Radius; public funtionArea(int radius) { return this.radius * this._PI } } class program { public static void main() {   Circle c1 = new Cirle();   float area1 = c1.functionRaduis(5); Circle c2 = new Cirle();   float area2 = c1.functionRaduis(6); } } 

Now here we have created 2 instances for our class circle , i.e 2 sets of copies of _PI along with other variables are created. So say if we have lots of instances of this class multiple copies of _PI will be created occupying memory. So in such cases it is better to make such variables like _PI static and operate on them.

class circle { static float _PI =3.14F; public int Radius; public funtionArea(int radius) { return this.radius * Circle._PI } } class program { public static void main() {   Circle c1 = new Cirle();   float area1 = c1.functionRaduis(5); Circle c2 = new Cirle();   float area2 = c1.functionRaduis(6); } } 

Now no matter how many instances are made for the class circle , only one copy exists of variable _PI saving our memory.

Source Link
TomHardy
  • 117
  • 1
  • 4

Static variables are used when only one copy of it is required. Let me explain this with an example:-

class circle { public float _PI =3.14F; public int Radius; public funtionArea(int radius) { return this.radius * this._PI } } class program { public static void main() { Circle c1 = new Cirle(); float area1 = c1.functionRaduis(5); Circle c2 = new Cirle(); float area2 = c1.functionRaduis(6); } } 

Now here we have created 2 instances for our class circle , i.e 2 sets of copies of _PI along with other variables are created. So say if we have lots of instances of this class multiple copies of _PI will be created occupying memory.So in such cases it is better to make such variables like _PI static and operate on them.

 class circle { static float _PI =3.14F; public int Radius; public funtionArea(int radius) { return this.radius * Circle._PI } } class program { public static void main() { Circle c1 = new Cirle(); float area1 = c1.functionRaduis(5); Circle c2 = new Cirle(); float area2 = c1.functionRaduis(6); } } 

Now no matter how many instances are made for the class circle , only one copy exists of variable _PI saving our memory.