开发者

constructors in java

开发者 https://www.devze.com 2023-04-02 20:30 出处:网络
public class A { public A() { System.out.println(\"a1\"); } public A(int x) { System.out.println(\"a2\"); }}
public class A {
public A() {
    System.out.println("a1");
}
public A(int x) {
    System.out.println("a2");

}}

public class B extends A {
public B() {
    super(5);
    System.out.println("b1");
}
public B(int x) {
    this();
    System.out.println("b2");
}
public B(int x, int y) {
    super(x);开发者_如何学编程
    System.out.println("b3");
}}

I don't understand why the default constructure of A is not applied when I run B b= new B();

B extends A, so First we call the constrcture of A that supposed to print "a1", and then we call the the second constructure of A which prints "a2" and B() prints "b1", but when I run it, it prints only "a2,b1", so obviously A() wan't applied at the beginning- why?


When you say B b = new B(); you are calling the default constructor which is

public B() {
    super(5);
    System.out.println("b1");
}

Since this already has a call to its super constructor [super(5)] so the compiler will not insert the implicit default constructor. Hence the result.

NOTE: From your question, it seems that you have the idea that all the constructors are called when you create an object. I am afraid that is incorrect. Only that constructor will be called which you explicitly call to create the object. And if that constructor calls other constructor via the this() method, only then the other constructors will be called.


B extends A, so First we call the constrcture of A that supposed to print "a1"

This statement is incorrect. In class B your no arguments constructor

public B() {
    super(5);
    System.out.println("b1");
}

calls the constructor of the superclass (class A) which takes an int parameter.

public A(int x) {
    System.out.println("a2");
}

You never make a call to super() so the constructor that prints "a1" will not be called when you call any of B's constructors

Calling a super constructor must be the first line of a constructor. If you wish to call the no argument constructor of a superclass (in this case, the one that prints "a1"), you would write...

public B() {
    super();
    System.out.println("b1");
}

If you do not specify calling a super constructor, then java will automatically put in a call to the no argument super constructor.


That's because you call super(5) in B constructor which call the second A constructor instead of the first one.


I think you missunderstand how Java handles constructors. First of all, by default Java will call only one constructor per class, unless you explicitly tell it to call more using this(...). Secondly, that one construct that is called is the default constructor of the super class (so it will call super(), not this()); so class A actually looks like this:

public class A {
  public A() {
     super(); // implicit call to super(), which is Object()
     System.out.println("a1");
  }
  public A(int x) {
    super(); // implicit call to super(), which is Object()
    System.out.println("a2");
  }
}

So invoking A() will implicitly call Object(), however invoking A(int x) will also implicitly call Object(), and not - how you seem to assume - A().

Since in B you always explicitly specify to call another constructor, the compiler will not add anything. Below I added comments on what will happen on the calls to super(...) and this(...)

   public class B extends A {
      public B() {
        super(5); // explicit call to A(5), no implict call to A()
        System.out.println("b1");
      }
      public B(int x) {
        this(); // explicit call to B(), no implicit call to A()
        System.out.println("b2");
      }
      public B(int x, int y) {
        super(x); // explict call to A(x), no implicit call to A()
        System.out.println("b3");
      }
    }

So again, the important thing to remember is that Java will insert a call to super() in the first line of any constructor, unless you explicitly invoke another constructor using this(...) or super(...). It will never insert this() by itself.

0

精彩评论

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

关注公众号