1

I am trying to do the exercise "reusing/E07_SimpleInheritance2" in "Thingking in Java 4th edition". the code works but the output in the console is:

A: New instance C
B: New instance B
C: New instance C

But I think C should be in front of B because the sentence "System.out.println('C: ' + str);" is in the C2's constructor, followed by instance B. Well, why not?

OK, I just realized when initialize, the sequences is: (Static variables, static fields) > (variables, fields) > Constructors. That's the reason. Problem solved, thanks for the guys bellow :)

class A2{
    A2(String str){
        System.out.println("A: " + str);
    }
}

class B2{
    B2(String str){
        System.out.println("B: " + str);
    }
}

class C2 extends A2{
    C2(String str){
        super(str);
        System.out.println("C: " + str); //I think it should work first
    }
    B2 b = new B2("New instance B"); //Then followed by B
}
public class Q7_7_SimpleInheritance2 {

    public static void main(String[] args) {
        C2 c = new C2("New instance C");

    }

}
2
  • please add code for instantiation of your objects Commented Jun 19, 2018 at 9:52
  • @DawoodibnKareem Ooops, I misread. Thought his problem was why "A" came before "B". Commented Jun 19, 2018 at 11:05

3 Answers 3

4

When a C2 instance is created, the first thing to happen is executing the A2 (super class) constructor, which results in A: New instance C being printed first.

Then the instance variables of C2 are initialized, before the body of C2's constructor. Therefore B2 b = new B2("New instance B"); is executed before System.out.println("C: " + str);, so B: New instance B is the second output line and C: New instance C the final line.

You may have been confused by the instance variable b being declared after the constructor, but that has no meaning.

Changing the code as follows may be less confusing, but would yield the exact same output:

class C2 extends A2 
{
    B2 b = new B2("New instance B");
    C2(String str){
        super(str);
        System.out.println("C: " + str);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes. I just realized when initialize, the sequences is: (Static variables, static fields) > (variables, fields) > Constructors. So whatever the b being declared...
2

The key point here is that when instantiating a class the following happens(in this order):

  1. super is called
  2. class members are initialized
  3. other constructor statements are executed

2 and 3 explain why B: New instance B is printed before C: New instance C.

Comments

-1

A is before C because the constructor of C calls the constructor of A before writing to sysout.

1 Comment

The question was why is B before C.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.