How to store the latest values ​​in a variable in Java programming?

In my java program, I need to save the last values ​​in a variable, and my code is shown below

public class Exmp2 { int noOfInstances; public Exmp2() { noOfInstances++; } public static void main(String[] args){ Exmp2 e1=new Exmp2(); System.out.println("No. of instances for sv1 : " + e1.noOfInstances); Exmp2 e2=new Exmp2(); System.out.println("No. of instances for sv1 : " + e2.noOfInstances); System.out.println("No. of instances for st2 : " + e2.noOfInstances); Exmp2 e3=new Exmp2(); System.out.println("No. of instances for sv1 : " + e3.noOfInstances); System.out.println("No. of instances for sv2 : " + e3.noOfInstances); System.out.println("No. of instances for sv3 : " + e3.noOfInstances); } } 

My output should be 1 2 2 3 3 3 but I get 1 1 1 1 1 1 1 can you give a solution?

+4
source share
9 answers

Declare the variable noOfInstances as static .

 static int noOfInstances; 

Since its not static , for each new Exmp2() a noOfInstances is created for this instance with a default value of 0 .

+7
source

you must declare noOfInstances static

  static int noOfInstances; 

otherwise, each new instance created using new will have its own noOfInstances value, starting at 0 again

+3
source

noOfInstances must be declared static. For instance:

 static int noOfInstances; 

This might be an interesting read. It should have an example with a similar situation like yours:

static keyword

In short, static literally turns a variable shared between instances of a given class. If you have a variable that is not static, each instance will have its own value for that variable. Although a statically declared variable will have the same value in all instances of the class.

+2
source

The variable must be declared static.

Why? . Since currently your variable noOfInstances not static, and for each instance of your class you create a variable noOfInstances, and it will always be 1. Thus, declaring it static, it is shared between all instances of this class and will have the correct value.

Static variables are created when a class is loaded and shared in all instances.

+2
source

Make noOfInstances static as follows,

 static int noOfInstances; // static are shared among all objects created. 

and you don’t need to call e1.noOfInstances , instead you can call Exmp2.noOfInstances

An instance (non-static) variable is copied to objects, while static variables are not copied to the object. static are at the class level. Each object can see it.

+2
source

You must declare your variable int noOfInstances; as static , in your code the instances default to 0.

+2
source

You must declare the variable static : static always saves the last values, its static variable will be stored in the static pool

 static int noOfInstances; 
+2
source

Whenever you execute new Exmp2(); and then noOfInstances is set to 0 when creating a new object, make noOfInstances as static so that its area shifts to the class level.

+1
source

See the example of using a static variable for the count instance below.

 package com.stackoverflow.test; public class Exmp2 { static int noOfInstances; public Exmp2() { noOfInstances++; } public static void main(String[] args) { System.out.println("No. of instances at this point " + Exmp2.noOfInstances); Exmp2 e1 = new Exmp2(); System.out.println("No. of instances at this point " + Exmp2.noOfInstances); Exmp2 e2 = new Exmp2(); System.out.println("No. of instances at this point " + Exmp2.noOfInstances); Exmp2 e3 = new Exmp2(); System.out.println("No. of instances at this point " + Exmp2.noOfInstances); } } 
+1
source

All Articles