0

hi i want to understand the flow of execution of this program.i am new to programming and learning constructors in java.

My doubt is when object obj calls the constructor and assigns the value 10 and jack to x and y, then isn't it that when the next object obj2 is created it will replace the value with 12 and matt in x and y ? so since the call() method is called after that isn't it suppose to print 12 and matt to both the calls as the print is printing x and y ?

class Constructor2 {
  int x;
  String y;

  Constructor2(int i, String s){
    x = i;
    y = s;
  }

  void call(){
    System.out.println("roll no is "+ x + " name is " + y);
  }

  public static void main(String args[]) {
    Constructor2 obj = new Constructor2(10, " jack ");
    Constructor2 obj2 = new Constructor2(12, " matt ");

    obj.call();
    obj2.call();
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
John
  • 19
  • 3
  • 3
    Each `Constructor2` object has its own copy of those variables. Modifying them in one doesn't affect the other's. – resueman Feb 18 '17 at 18:41
  • 2
    As long as things are not `static`, each object has it's own x and y. This has nothing to do with the constructor. – Thomas Weller Feb 18 '17 at 18:43
  • ok so that means ojb 1 will have x = 10 and s = jack and ojb2 will have x = 12 and s = matt so now when any method is being called using these objects it will use the values stored in the objects, right ? – John Feb 18 '17 at 18:50
  • @John that is correct. See [This Stackoverflow Question](http://stackoverflow.com/questions/15486392/what-is-the-difference-between-an-instance-and-a-class-static-variable-in-java) for some more explanation on the difference between instance and class variables in java. – James C. Taylor IV Feb 18 '17 at 19:29

4 Answers4

1

A class is like a template that describes how to build boxes.

Each time you call new, that template is looked at, and one new box is created, and some content is put into that box.

You are creating two boxes, and each time, you are putting different content into the freshly created box.

See here for further reading.

One key thing to understand: if you had used the static keyword when declaring a field, then all objects of that class would see the same value.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

The variables x and y are instance variables, that's why they are linked to the objects created. They don't affect to other objects or the class.

If you had made them to static variables, so called class variables, like..

static int x;
static String y;

..the content would be overwritten as you are afraid of.

0

Because every time you create a new instance of your object :

Constructor2 obj = new Constructor2(10, " jack ");
Constructor2 obj2 = new Constructor2(12, " matt ");

So it will reserve a space in memory for every Object obj and obj2 for that you get different value and this is logic.

You can take a look here : Working with Objects in Java

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

Whenever object is initialized, it will call constructor first ,then variable are initialized.

so call method will have the update values.