1

when we create any object it will create a default constructor and call super()

example:

    Parent(){
        super();
    }

but when I look to Object class there is no constructor in it, what will happen at the time of invoking super() constructor.

it seems JVM calling method which is not at all exist in Object class, but not able to understand what is happening.

Sravan
  • 591
  • 1
  • 4
  • 16
  • 2
    Most of the implementation of `java.lang.Object` is native. – Mena Nov 06 '15 at 14:11
  • Are you looking at the source code or the class file? The java compiler will insert an empty constructor into the `Object` class file, just like it does with your `Parent` class. – SamTebbs33 Nov 06 '15 at 14:15
  • Thanks @Mena for the quick reply :) Can we get more info/links regarding this, what is happening in native implementation. – Sravan Nov 06 '15 at 14:16
  • @SamTebbs33 I looked in to both source code and .class (decompiled) of Object class – Sravan Nov 06 '15 at 14:17
  • 1
    @Sravan try [this](http://stackoverflow.com/questions/2292629/where-to-find-source-code-for-java-lang-native-methods) question for some insights on native source. – Mena Nov 06 '15 at 14:18
  • @Mena thanks for the info, will look in to this and get back to here if something else needed :) – Sravan Nov 06 '15 at 14:25
  • @Sravan you're welcome. Good hunt! – Mena Nov 06 '15 at 14:26

4 Answers4

1

The class Object performs some native operations in each instance.

Java compiler guarantee that every object created calls Object constructor.

That's the reason why every constructor has to start with a super() or a this() call. So the first constructor executed in every instance will be Object constructor.

jfcorugedo
  • 9,793
  • 8
  • 39
  • 47
  • Thanks @jfcorugedo for quick response :) Can we get more info/links regarding this, what is happening in native implementation. – Sravan Nov 06 '15 at 14:21
  • 1
    Well, it depends on each JVM, but basically it allocates enough memory for this object – jfcorugedo Nov 06 '15 at 14:25
1

Object class is mostly implemented in a native way, such that constructor doesn't call its parent. Furthermore it doesn't have any parent class because it is set to null in .class file. You can read more at java .class file format about how Java class is stored.

Java language itself doesn't allow such tricks, but they are allowed by VM, as for every internal class file, validation is disabled.

Kamil Jarosz
  • 2,168
  • 2
  • 19
  • 30
1

The creation of objects is down to the JVM. Every constructor in Java appears as a special method named <init> which is responsible for instance initializing. This <init> method is supplied by the compiler and because <init> is not a valid identifier in Java, it cannot be used directly in the language.

How does the JVM invoke this <init> method?

The JVM will invoke the <init> method using the invokespecial instruction and can only be invoked on uninitialized class instances.

What will happen in the Object class constructor?

Nothing more than would happen in a subclass that has a default empty constructor (minus the call to super()).

The default constructor can be explicitly defined, if not the compiler will put it in for you as long as no other constructor is already defined.

For more information take a look at the JVM specification and the Java Language Specification:

kstandell
  • 775
  • 6
  • 16
1

While all the other answers about native code are true, formally there is nothing special or mysterious about java.lang.Object's apparent lack of a constructor.

Any class that lacks an explicitly defined constructor will automatically receive a default constructor. This is valid code for example:

class Foo {}
class Bar extends Foo {
   public Bar(String baz) {
      super();
   }
}

Where Object is different is what that default constructor contains:

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

biziclop
  • 48,926
  • 12
  • 77
  • 104