Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Rules for Constructors - K&B Page 315 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Rules for Constructors - K&B Page 315" Watch "Rules for Constructors - K&B Page 315" New topic
Author

Rules for Constructors - K&B Page 315

Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Hi all, this is taken from page 315 of K&B 1.4:
____________________________________________________________________________

1) "A private constructor means only code within the class itself can instantiate an object of that type, so if the private-constructor class wants to allows an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class."

Question: Why is it that only static method or static variable can access an instance created from the class with private constructors?
____________________________________________________________________________

2) "A no-arg constructor is not necessarily the default constuctor, although the default constructor is always a no-arg constructor."

Question: When I read this, I was thinking... default constructor is always a no-arg constructor... so default constructor = no-arg constructor. But the first sentence threw me off... "A no-arg constructor is not necessarily the default constuctor".
____________________________________________________________________________

3) "The only way a constructor can be invoked is from within another constructor. In other words, you can't write code that actually calls a constructor as follows:

class Horse {
Horse() {} // constructor
void doStuff() {
Horse(); // calling the constructor - illegal!
}
}"

Question: Just want to check if my thoughts are correct here. The way to invoke Horse constructor is:

Horse h = new Horse();

So as to the statement, "The only way a constructor can be invoked is from within another constructor.", what does the invoke from within another constructor means here? That Horse is invoke from Animal which is in turn invoked from the Object constructor?
____________________________________________________________________________

Thank you.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Shanel Jacob:
Hi all, this is taken from page 315 of K&B 1.4:
____________________________________________________________________________

1) "A private constructor means only code within the class itself can instantiate an object of that type, so if the private-constructor class wants to allows an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class."

Question: Why is it that only static method or static variable can access an instance created from the class with private constructors?[QB]


The point here is not just that it is static, but that it is part of the class definition. A static method will be able to invoke the private constructor because it is part of the class definition.

[QB]2) "A no-arg constructor is not necessarily the default constuctor, although the default constructor is always a no-arg constructor."

Question: When I read this, I was thinking... default constructor is always a no-arg constructor... so default constructor = no-arg constructor. But the first sentence threw me off... "A no-arg constructor is not necessarily the default constuctor". [QB]


A default constructor is one that is generated when no constructor exists in the class definition. If you create a no-arg constructor, it is not considered a default constructor.

[QB]3) "The only way a constructor can be invoked is from within another constructor. In other words, you can't write code that actually calls a constructor as follows:

class Horse {
Horse() {} // constructor
void doStuff() {
Horse(); // calling the constructor - illegal!
}
}"

Question: Just want to check if my thoughts are correct here. The way to invoke Horse constructor is:

Horse h = new Horse();

So as to the statement, "The only way a constructor can be invoked is from within another constructor.", what does the invoke from within another constructor means here? That Horse is invoke from Animal which is in turn invoked from the Object constructor?
____________________________________________________________________________

Thank you.


What they mean here is that you can invoke one constructor of a class from within another using the keyword this, but you can't do that from within another method.

Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Hi, thank you for replying.
________________________________________________________________________

"The point here is not just that it is static, but that it is part of the class definition. A static method will be able to invoke the private constructor because it is part of the class definition."

So anything that is Private or Static defined within THE class, are considered part of the THE class definition?
________________________________________________________________________

public class Test // create a class Test
{

private Test() { } // make a private constructor for Test

public Test(int num) { this(); } // make a public constructor that takes in "int"

}

The book says on page 324 that "this() always means a call to another constructor in the same class." So are you calling the private Test from the public Test? If so, what happens if you have like 4 or 5 Test constructors, how do we know which one this() is trying to call?

Also, can I assume that the 2 way you can call/invoke a constructor is:

1) Like you have shown me above, using this() from a constructor to call another constructor within that class

2) use the "new" keyword
________________________________________________________________________

Thank you.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Shanel Jacob:
Hi, thank you for replying.
________________________________________________________________________

"The point here is not just that it is static, but that it is part of the class definition. A static method will be able to invoke the private constructor because it is part of the class definition."

So anything that is Private or Static defined within THE class, are considered part of the THE class definition?


Yes the methods that are defined inside the class definition are considered part of it. So the following is legal.



The book says on page 324 that "this() always means a call to another constructor in the same class." So are you calling the private Test from the public Test? If so, what happens if you have like 4 or 5 Test constructors, how do we know which one this() is trying to call?


Constructors are dinstinguished from each other by their signatures like other methods.

Also, can I assume that the 2 way you can call/invoke a constructor is:

1) Like you have shown me above, using this() from a constructor to call another constructor within that class

2) use the "new" keyword
________________________________________________________________________

Thank you.


If the class definition is a superclass of another class definition, then the subclass' constructors can invoke one of the constructors of the superclass by using the keyword super.

Also if a constructor of a subclass does not explicitly call a constructor from its direct superclass, then an attempt is made to call a no-arg constructor from the superclass.
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
How do you make a private constructor in the first place?

We cannot typed: "private Horse()" because that will make a private method instead so how do we make a private Horse constructor?
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
"private Horse()"

Never mind, just realized that there's no return type. So its not a method but a private constructor
 
 
subject: Rules for Constructors - K&B Page 315
 
Similar Threads
Why Is a No-Arg Constructor Required Here?
constructor doubt
constructors
Is it possible for a class to have a private constuctor???