1

I'm little confused about static methods and object creation in java.

As we know we can access static members in static method as here.

public static void main(String[] args){
// only static method from outside ( without any object )
}

But my stupid question is that why java allow this?

`public static void main(String[] args){
    Object o = new Object(); // is constructor implicitly static? 
                          // I'm sure no but why java allow this to call here?
    }

I know the above statement is similar to declare local variable in static method.

public static void main(String[] args){
 int a = 3;
}

But I'm little confused about constructor.

4
  • The new operator creates the object before calling the constructor for that object. Hence the constructor is executed on an existing object, not statically on any class. Commented Apr 26, 2016 at 14:44
  • @AndreasFester then why mostly people say we are calling constructor by this new Object(); Commented Apr 26, 2016 at 14:46
  • 1
    But that is what I said - you can treat the object creation as a two step process which is performed by new. See the answer from @PeterLawrey. Commented Apr 26, 2016 at 14:48
  • @LetDoit it is called the constructor. Commented Apr 26, 2016 at 14:51

2 Answers 2

2

In bytecode, your main() method looks like this (result of the javap -c Main.class command):

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

As you can see, at location 0, the new instruction is performed. Then, at location 4, the constructor is invoked on the newly created object.

This is also specified in the Java Virtual Machine Specification:

4.10.2.4. Instance Initialization Methods and Newly Created Objects

Creating a new class instance is a multistep process. The statement:

...
new myClass(i, j, k);
...

can be implemented by the following:

...
new #1            // Allocate uninitialized space for myClass
dup               // Duplicate object on the operand stack
iload_1           // Push i
iload_2           // Push j
iload_3           // Push k
invokespecial #5  // Invoke myClass.<init>
...
Sign up to request clarification or add additional context in comments.

2 Comments

You can also see the constructor has the internal signature <init> with a return type of void. +1
@LetDoit Use javap -c -v Most IDEs have a byte code viewer.
2

Constructors are not static. They are called on the instance, you just created. In byte code what happens.

  1. An new object is created, but it is not inilialised.
  2. The constructor is called on that object. In the constructor this is the object being initialised.

6 Comments

An new object is created, but it is not inilialised. In byte code? Object already created? or when we call new ?
yes, no, yes. When we use new it creates a new object, after the object is created, it is initialised in the constructor.
are outer classes implicitly static?
@LetDoit outer classes implicitly have no even-more-outer class to be an inner class of.
Then don't know why people give +4 to this answer. stackoverflow.com/a/18738063/2769917 Where he clearly mentioned. that An outer class is already implicitly static.
|

Your Answer

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