Increments by 1 and adds it to each created object?

class MyObject { static int instanceCounter = 0; static int counter = 0; MyObject() { instanceCounter++; counter = counter + 1; } } 

I use static ints to get this output:

InstanceCounter value for object 1: 5

Counter Instance Value for MyObject: 5

The counter value for the object is 1: 1

The value of the counter for the object 2: 2

Counter value for an object 3: 3

The value of the counter for the object 4: 4

Counter value for object 5: 5

but its display

InstanceCounter value for object 1: 5

Counter Instance Value for MyObject: 5

The counter value for the object is 1: 5

The value of the counter for the object 2: 5

Counter value for object 3: 5

Counter value for an object 4: 5

Counter value for object 5: 5

my runner class

 class RunMyObject { public static void main(String[] args) { MyObject Object1 = new MyObject(); MyObject Object2 = new MyObject(); MyObject Object3 = new MyObject(); MyObject Object4 = new MyObject(); MyObject Object5 = new MyObject(); System.out.println("Value of instanceCounter for Object 1: " + Object1.instanceCounter); System.out.println("Value of instanceCounter for MyObject: " + MyObject.instanceCounter); System.out.println("Value of Counter for Object 1: " + Object1.counter); System.out.println("Value of Counter for Object 2: " + Object2.counter); System.out.println("Value of Counter for Object 3: " + Object3.counter); System.out.println("Value of Counter for Object 4: " + Object4.counter); System.out.println("Value of Counter for Object 5: " + Object5.counter); } } 

and if I delete static, this is what it displays

InstanceCounter value for object 1: 5

Counter Instance Value for MyObject: 5

The counter value for the object is 1: 1

Counter value for object 2: 1

Counter value for object 3: 1

Counter value for object 4: 1

The counter value for the object is 5: 1

+4
source share
3 answers

Since instanceCounter is a static variable, all objects have the same variable. Since you increase the instance instance with each object construction, at the end of creating 5 objects its value is 5. Therefore, you get the result as 5 in all your sys out. Static point

EDIT
To accomplish what you need, follow these steps:

 class MyObject { static int instanceCounter = 0; int counter = 0; MyObject() { instanceCounter++; counter = instanceCounter; } } 
+14
source

You should confuse the use of static variables .

static class variables are created once for each class. They are shared by all instances of the class, where non-static class variables are created for each instance of the object.

So, if your variable is counter static , it will be created only once and will be used by all instances of your class.

When you access it using MyObject.counter or object1.counter , etc., you get access to the same counter variable that static variables with the class name can access, as well as with the instance variable.

And if it is non-static , and each instance (or object) of your class will have its own copy of counter .
So each of your Object1 , Object2 , etc. Will have its own variable counter .

And they will all have a value of 1, and so you get 1 on the output.

UPDATE:

Modify your code to get the desired result that you mentioned in a comment on one of the answers:

 MyObject Object1 = new MyObject(); System.out.println("Value of Counter for Object 1: " + Object1.counter); MyObject Object2 = new MyObject(); System.out.println("Value of Counter for Object 2: " + Object2.counter); MyObject Object3 = new MyObject(); System.out.println("Value of Counter for Object 3: " + Object3.counter); MyObject Object4 = new MyObject(); System.out.println("Value of Counter for Object 4: " + Object4.counter); MyObject Object5 = new MyObject(); System.out.println("Value of Counter for Object 5: " + Object5.counter); System.out.println("Value of instanceCounter for Object 1: " + Object1.instanceCounter); System.out.println("Value of instanceCounter for MyObject: " + MyObject.instanceCounter); 
+2
source

If you want to do it like this, add another variable: ie:

 class MyObject { int instanceCounter = 0; static int counter = 0; MyObject() { instanceCounter = counter; counter++; } } 
0
source

Source: https://habr.com/ru/post/1446642/


All Articles