Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Fri Jan 20, 2017 2:20 pm
by uqzma1xg
I thought that A2 println method is an unreachable statement.
because this(); will call another constructor.
someone can explain me please the steps when calling A with String argument?
thanks

Code: Select all

class A{
public A() {} // A1
public A(String s) { this(); System.out.println("A :"+s); } // A2
}

Re: About Question enthuware.ocajp.i.2.1209 :

Posted: Fri Jan 20, 2017 10:49 pm
by admin
It is not unreachable. After the call to this() finishes, the control will come back to next line after this();.

This is same as calling any method. Statements written after a method call are not unreachable. They will be executed after the method call returns.

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Wed Aug 29, 2018 3:01 am
by RobinDRG
Maybe is for my english, but I understood creating objects inside the class C, hence there are 3 forms.
"Class C can be instantiated only in two ways by users of class C."

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Wed Aug 29, 2018 4:21 am
by admin
"user of a class" means some other code that uses this class. It cannot be inside the class, because the one who writes code inside the class is the developer of that class, not the user.

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Mon Nov 26, 2018 2:48 pm
by OCAJO1
"At least one of the constructors of each class is called as a result of constructing an object of class C."

Would someone please list an example (or examples) of how at least one constructor of each class gets called? Thanks

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Mon Nov 26, 2018 8:46 pm
by admin
Class C has three constructors, which means you can create an object of class C using any of the three constructors. For example, if you create an object of C using new C(), then the C1 constructor of C is invoked. This has a call to super(), which means, the default no-args constructor of B (not shown in the code) is invoked. This default constructor of B contains a call to super(), which means, constructor A1 of A is invoked. Thus, you can see that one constructor of each class is invoked.

You can now work out the path for other constructors of C in the same manner.

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Mon Nov 26, 2018 9:20 pm
by OCAJO1
Thanks for the reply.

Am I correct in saying that since the 2nd one has this() in the first line, it doesn't need to go looking in B or A class?

I guess what I'm not seeing is the 3rd constructor's path. I don't see the super for this one, (public C(int i) {}) in B or A classes. So how does this one work?

Thanks

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Mon Nov 26, 2018 9:26 pm
by admin
Your question requires too many fundamental details about constructors to be explained in post. You need to go through the fundamentals of constructors from a good book. Once you do that your doubt will be cleared immediately. Section 9.3 of Hanumant's OCAJP 8 Fundamentals covers this nicely.

Re: About Question enthuware.ocajp.i.v8.2.1209 :

Posted: Tue Nov 27, 2018 2:53 pm
by OCAJO1
I'm glad you recommended this book. I've read in a couple of other books about the relationship between the constructors, but section 9.3 of this book really drives it home. Thanks