Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increments by 1 and add it with every created object? [closed]

Tags:

java

oop

class MyObject {

static int instanceCounter = 0;

static int counter = 0;

MyObject() {

instanceCounter++;

counter = counter + 1;

}

}

I am using the static ints to get this output:

Value of instanceCounter for Object 1: 5

Value of instanceCounter for MyObject: 5

Value of Counter for Object 1: 1

Value of Counter for Object 2: 2

Value of Counter for Object 3: 3

Value of Counter for Object 4: 4

Value of Counter for Object 5: 5

but its displaying

Value of instanceCounter for Object 1: 5

Value of instanceCounter for MyObject: 5

Value of Counter for Object 1: 5

Value of Counter for Object 2: 5

Value of Counter for Object 3: 5

Value of Counter for Object 4: 5

Value of Counter 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 remove static this is what it displays

Value of instanceCounter for Object 1: 5

Value of instanceCounter for MyObject: 5

Value of Counter for Object 1: 1

Value of Counter for Object 2: 1

Value of Counter for Object 3: 1

Value of Counter for Object 4: 1

Value of Counter for Object 5: 1

like image 564
Mansoor Akram Avatar asked Nov 17 '12 13:11

Mansoor Akram


People also ask

How do you add increments in Java?

There are two ways to use the increment operator; prefix and postfix increment. The prefix increment looks like ++variablename; while the postfix increment looks like variablename++; . Both of these operations add one to the value in the variable.

Can we increment object in Java?

You can't do that in Java.

How do you increment a static variable?

When we create objects of our Counter class in main, and access the static variable. The outout is 2, because the COUNT variable is static and gets incremented by one each time a new object of the Counter class is created. You can also access the static variable using any object of that class, such as c1. COUNT .


2 Answers

Since instanceCounter is a static variable, all objects share the same variable. Since you are incrementing the instanceCounter during each object construction, at the end of creating 5 objects, its value is 5. Consequently you get the output as 5 in all your sys outs. Thats the point of static

EDIT
To achieve what you need, do the following:

class MyObject  {

    static int instanceCounter = 0;

    int counter = 0;

    MyObject() 
    {
        instanceCounter++;
        counter = instanceCounter;
    }

}
like image 117
Raam Avatar answered Nov 15 '22 19:11

Raam


If you want to do it like this, you should add another variable, i.e.:

class MyObject {

    int instanceCounter = 0;

    static int counter = 0;

    MyObject() {

        instanceCounter = counter;
        counter++;
    }
}
like image 24
Daniel Cisek Avatar answered Nov 15 '22 20:11

Daniel Cisek