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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
uqzma1xg
Posts: 6
Joined: Fri Jan 20, 2017 8:45 am
Contact:

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

Post 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
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

RobinDRG
Posts: 6
Joined: Sat Jun 23, 2018 10:17 am
Contact:

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

Post 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."

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests