开发者

How does this object get created?

开发者 https://www.devze.com 2023-04-07 02:45 出处:网络
Many articles say objects get created only after the class\'s constructor gets called. But I found this snippet and it works fine.

Many articles say objects get created only after the class's constructor gets called. But I found this snippet and it works fine.

public class A {

    public A(){
        this.foo();//line #1
    }

    p开发者_JS百科rivate void foo() {
        System.out.print("without an instance..!!!!");

    }
}

class B extends A
{
    public static void main(String[] args){

    A a = new A();  //line #2
    }
    }

Here you see, I'm trying to create an object of its super class in line #2 and in its constructor how come its method got called without an instance. What's going on here, is Constructer of instance of A is getting called here.?


The constructor is always called when an object is created. Even if you don't explicitly define a constructor, the compiler will generate one for you with an empty body.

You may call other methods of the class from the constructor. All non-static methods get an implicit (compiler generated) parameter to this, the actual class instance. However, it is important to know that while executing the constructor, the object is not yet fully created, although all data members of the class in question (if there are such) have already been initialized, at least to some default value. Because of this, you

  • should not publish this (i.e. pass it to other objects / threads) before exiting the constructor call, and
  • you should not call non-final, non-private methods from the constructor.

Doing either of these (in a non-final class) means that you give access to an object not yet fully constructed, which may result in subtle, hard to find bugs later. E.g. if the virtual method in question is overridden in a subclass and the implementation depends on some member defined and initialized only in the subclass constructor, the method gets called before the subclass member is correctly initialized, thus it won't have the value you would expect.


Because public static void main is the entry point of the programm. So you do not require to create instance of the class B.

The method signature for the main() method contains three modifiers:

* public indicates that the main() method can be called by any object.
* static indicates that the main() method is a class method.
* void indicates that the main() method has no return value.

Read more : Understanding public static void main function

So that Constructor of A is get called when the programm get executed and that it calls the foo method of the super class A.


It's not called without an instance. You call it on this - that's an instance.


The constructor of A is getting called because you called it directly. However, if you wished to call A through B, within Main [which is called without a current instance of the containing class B (1 because its static, and 2. its reserved for the beginning of the application)] you would just change the "new A()" to "new B()"

Since you're using a constructor with no parameters, a default constructor in automatically generated at compile time.


Whether main() is the entry point or not is not the explanation. The reason is that main() is static, therefore doesn't require an instance of its class.

No instance of B is ever created by this program.


class A is public, and its inherited by class B. class B can instantiate class A, with

A object=new A() 

and the object initialization is done by Constructor method defined automatically. Constructor of A in turn calls method foo(), which is private to class A. As far as i know, to call a method from a class which is in the same class scope, no instance is needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号