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.

  • 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. – Andreas Fester Apr 26 '16 at 14:44
  • @AndreasFester then why mostly people say we are calling constructor by this `new Object();` –  Apr 26 '16 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. – Andreas Fester Apr 26 '16 at 14:48
  • @LetDoit it is called the constructor. – Peter Lawrey Apr 26 '16 at 14:51

2 Answers2

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.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • `An new object is created, but it is not inilialised.` In byte code? Object already created? or when we call `new` ? –  Apr 26 '16 at 14:48
  • 2
    yes, no, yes. When we use `new` it creates a new object, after the object is created, it is initialised in the constructor. – Peter Lawrey Apr 26 '16 at 14:50
  • are outer classes implicitly static? –  Apr 26 '16 at 14:57
  • @LetDoit outer classes implicitly have no even-more-outer class to be an inner class of. – Peter Lawrey Apr 26 '16 at 14:58
  • Then don't know why people give +4 to this answer. http://stackoverflow.com/a/18738063/2769917 Where he clearly mentioned. that `An outer class is already implicitly static.` –  Apr 26 '16 at 15:01
  • @LetDoit that is one way of explaining it. Top level classes existed before nested classes, and top level classes never need to explain why they were not nested, as there was no other option. – Peter Lawrey Apr 26 '16 at 15:04
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>
...
Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123