10

I am aware that for every class initialization, each class will extends the object class. Does this mean the JVM will create an object for a custom class and the Object class? Can anyone explain this process of initialization of class very clearly.

Edit :

So if I extend any super class to subclass, does this super class occupies the same memory of subclass?

Anthony
  • 9,451
  • 9
  • 45
  • 72
developer
  • 9,116
  • 29
  • 91
  • 150

7 Answers7

11

Does it means JVM will create object for custom class and Object class?

No, it will only create an object for the custom class, but this object contains the Object class members (and the members of all other super classes).

Conceptually, you can think of the memory layout of one instance of custom class looking like this:

+============+
|Members of  |
|Object      |
+------------+
|Members of  |
|other super |
|classes     |
|  ...       |
+------------+
|Members of  |
|Custom class|
+============+

Essentially, there will be one block of memory allocated with the size of the custom class (which includes Object and all other super classes), and by calling the constructors for each of the super classes, the members of the super classes will be initialized.

See also

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • So if i extend any super class to subclass, does this superclass occupies the same memory of subclass? – developer Jul 23 '13 at 08:54
  • I would not say "the same memory", but the same block of memory - for example, assumed super class requires 10 bytes and sub class requires (additional) 8 bytes, then a block of 18 bytes is allocated, which contains the members of the super class in the first 10 bytes and the members of the sub class in the remaining 8 bytes. – Andreas Fester Jul 23 '13 at 08:56
9

It doesn't create 2 objects, it only creates one object of custom class . The super class constructor is called to initialize the fields of the super class as by definition the super class must be initialized to construct the sub class. But remember the created object is an instance of both the custom class as well as the java.lang.Object.

Read JLS 12.5 to know how it works. A snippet :

Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden

Suggested Reading:

Does invoking a constructor mean creating object?

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
5

Consider this-

public class SomeClass {

    public static void main(String... args){

       SomeClass someClass = new SomeClass();

    }

}

Let's compile this class-

javac SomeClass.java

Now, let's disassemble-

javap -c SomeClass

We get-

public class SomeClass {
  public SomeClass();
    Code:
       0: aload_0       
       1: invokespecial #1        // Method java/lang/Object."<init>":()V
       4: return        

  public static void main(java.lang.String...);
    Code:
       0: new           #2       // class SomeClass
       3: dup           
       4: invokespecial #3       // Method "<init>":()V
       7: astore_1      
       8: return        
}

As you can see there's only one new bytecode meaning only one object has been created in the heap.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1

Only one object of CustomClass will be created and not for Object class.

Suppose you have a class CustomClass. The invocation new CustomClass() will create the instance of CustomClass. The process goes as follows.

  1. The default no argument constructor of CustomClass gets invoked.
  2. There is a default call to super constructor, in this case, Object class no-argument constructor.
  3. After super class constructor has run, control goes back to CustomClass.
  4. Initialization blocks of CustomClass run after super no-arg constructor has completed.
  5. After initialization blocks, constructor of CustomClass completes and the instance of CustomClass is created.
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

That's how inheritance works : You only create an object which inherits the properties from the parents ones. So you create a single object, which has the same properties that takes from the Object class and the ones that are into the custom class.

For example, you have both Person and Student classes, both extending from Object. Student class also extends the Person one. Person has its properties, as name, surnames.. Appart from that Student class has its own properties, as college code, average marks..

When you create a Student a single Student is created, with Object's and Person's properties.

Object
    --Person (name, surname1, surname2)
        --Student (college number, average mark)

You can have a look to English Wikipedia's Inheritance (object-oriented-programming) article, to know more about the inheritance, not only in Java language.

Aritz
  • 30,971
  • 16
  • 136
  • 217
0

When ever your are using new operator it will create object for that class. For the parent class attributes it will initialize the variables inside the space of the created object.

Veera
  • 1,775
  • 2
  • 15
  • 19
0

The question was what happens when a class is initialized, not when an instance of this class is created with new.

For instance:

class Foo {
    static void bar() { .... }
    ... // other members
}

This class is initialized (but not instantiated), when Foo.bar() is called.

And I think if that happens then a Class<Foo> object is created, the same one that you get through Foo.class.

Ingo
  • 36,037
  • 5
  • 53
  • 100