Sahil Kumar
Sahil Kumar

Reputation: 49

how does this code give new id for each thing object. id should be equal to 1 all the time

class Thing {
    public static int count = 0;
    public int id = 0;

    public Thing() {
        id = count;
        count++;
    }

    public void ID() {
        System.out.println("This object has id: " + id);
    }
}

public class wth {

    public static void main(String[] args) {

        Thing thing1 = new Thing();
        Thing thing2 = new Thing();
        Thing thing3 = new Thing();

        thing1.ID();
        thing2.ID();
        thing3.ID();
    }
}

Shouldn't this code print 1
                          1

Here is what I understand of the code:

First of all, the thing1 object is created and the constructor Thing runs thus making id=0 and count=1. Then thing2 is created and again the constructor runs making id=1 and count=2.

Now thing1.ID() runs printing the value of id which should be 1 but instead its 0 please explain how is it 0.

Upvotes: 0

Views: 113

Answers (4)

Blasanka
Blasanka

Reputation: 22437

In your question:

value of id which should be 1 but instead its 0 please explain how is it 0.

What I understood, you asking for first time you create object it's value should be 1 and next time increment by 1. If it is asked for,

Reason:

When you create first object(thing1) you are calling default constructor(Thing()), which you have implemented. And in the first line you assigning count variable value to id variable which is 0. Then you increment count by one. It is not affecting to id variable first time. Then, you call the ID() method, it will print, value that it hold 0.

Solution:

public Thing() {
   id = count;
   count++;
}

move the count++; to first line. Should be:

public Thing() {
   count++;
   id = count;
}

Output become:

This object has id: 1
This object has id: 2
This object has id: 3

Because of static each time you create new object count will increment.

If you want to get outputt as 1 every time:

First you want to know about static keyword:

  • Attributes and methods(member of a class) can be defined as static.
  • static members do not belongs to an individual object.
  • static members are common to all the instances(objects of the same class).
  • static members are stores in static memory(a common memory location which can by everybody).

The static modifier indicates that the count variable(attribute) are common to all the Thing in the whole class rather than to an individual object.

To answer your question:

public static int count = 0;

As I mention above this count variable common for all the objects. That means when you create a different objects it's value not changed.

remove the static modifier, should be:

public int count = 0;

When you removed static modifier count variable become individual for all the objects that you are creating. And for each object it will print as follows:

This object has id: 0
This object has id: 0
This object has id: 0

If you move the count++; to first line.

public Thing() {
   count++;
   id = count;
}

Output become:

This object has id: 1
This object has id: 1
This object has id: 1

Upvotes: 0

vatbub
vatbub

Reputation: 3118

count is static, which is why it gets incremented each time you create a new instance of Thing but id isn't. That means that each instance of Thing, being thing1and thing2 will each have their own id.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

This code illustrates the way static works. count is shared among all objects of the class, so it gets incremented each time a new Thing object is instantiated.

Note: This will break in concurrent environments: if you run this from multiple threads, some objects may get the same id. A better implementation would use AtomicInteger:

class Thing{
    public static AtomicInteger count = new AtomicInteger(0);
    public int id;
    public Thing(){
        id=count.incrementAndGet();
    }
    ...
}

Upvotes: 1

AntonH
AntonH

Reputation: 6437

public static int count=0;

This line means that the variable count is linked to the class, not the object. So every time a new Thing is created, the incrementation of this variable is applied across all the objects instantiating the class, including new instances that are made.

You can find more info in the official doc/tutorial here.

Upvotes: 1

Related Questions