1

suppose with the following code, does JVM always create instance of Object class, since Object is parent for all the classes the in Java.

class MyTestclass{

   void printMe(String name){
        System.out.println('I am '+name);
   }
   public static void main(String args[]){
        MyTestclass obj = new MyTestclass();
        obj.printMe("stackOverFlow.com");
   }
}

Another class say TestClass2

class TestClass2{

   void printMe(String name){
        System.out.println('I am '+name);
   }
   public static void main(String args[]){
        MyTestclass obj = new MyTestclass();
        TestClass2 obj2 = new TestClass2();
        obj.printMe("stackOverFlow.com");
        obj2.printMe("stackOverFlow.com");
   }
}

Will JVM creates two object instances if i run these two classes?

1
  • In theory, the JVM can "allocate" objects on the stack, so in fact no objects get created as it effectively unpacks all the fields as local variables. See -XX:+UseEscapeAnalysis In practice it rarely does this. Commented Oct 2, 2014 at 11:34

3 Answers 3

2

No. There is only one object created and all the memory allocated in the name of Child, even for the field inherited from Parent.

does JVM always create instance of Object class, since Object is parent for all the classes the in Java.

No. But instead JVM calls the parent constructors until it reaches the top most Object constructor which calls as constructor chaining.

Sign up to request clarification or add additional context in comments.

Comments

2

Well... it depends what you mean by asking whether an instance of Object gets created...

Remember how inheritance works. Suppose that you have four classes: Object, Animal, Dog and Terrier. When you create an instance of Terrier

Terrier t = new Terrier();

only one instance gets created. You don't get a separate Object instance created. But on the other hand, the point of inheritance is that a Terrier is a Dog, and it is an Animal, and it is an Object. So this one instance that gets created isn't merely an Object, but it certainly is an Object, just like everything else.

So you could have written

Object t = new Terrier();

or

Animal t = new Terrier();

or

Dog t = new Terrier();

and it would have been perfectly valid Java.

So the answer is: a single instance of a subclass of Object gets created, and it thereby is an Object.

1 Comment

Yes , I meant how many instances of Object class will be present or created.
1

Only one object is created no matter what is the inheritance hierarchy is. Features from the parent are inherited and are provided in the object of class that is used with new operator.

  MyTestclass obj = new MyTestclass();

one object with all the features of MyTestclass and Object class in obj instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.