If you click on this stuff and give your money to some city slickers, JavaRanch gets a kickback. Hoooooooo Doggy!     
Do not post questions from the actual certification exams. If you see a question from an actual exam, please e-mail the moderators privately to let us know. To discuss a question from a mock exam, please mention which mock exam and the question number (if provided) so we know it's not from the real exam.



UBBFriend: Email This Page to Someone!
  JavaRanch Big Moose Saloon
  Programmer Certification
  ? on private constructors ........

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   ? on private constructors ........
eram
ranch hand
posted March 14, 2000 05:03 PM     Click Here to See the Profile for eram   Click Here to Email eram     Edit/Delete Message Reply w/Quote
Hi all,
I am new to this Ship. found great!

Can a constructor be declared private ?
a.)yes
b.)No

Can anyone expalin with an example.

My question is from the point of view of exam.

Math class cannot be instantiated(i guess private contsructor), but i could not find the contructor summary in the API.
Is there a place I can find this ?

IP: Logged

Howard Stern
ranch hand
posted March 14, 2000 05:12 PM     Click Here to See the Profile for Howard Stern   Click Here to Email Howard Stern     Edit/Delete Message Reply w/Quote
The answer is "yes". If you want any to prevent any class from being instantiated you should declare its constructor as private. Hence its constructor shall be applicable only within the class itself and not from any other class. This may apply to any class. Hope this makes it clear.

IP: Logged

eram
ranch hand
posted March 15, 2000 08:47 AM     Click Here to See the Profile for eram   Click Here to Email eram     Edit/Delete Message Reply w/Quote
Thanks .I got it.

IP: Logged

nirvan sage
greenhorn
posted March 16, 2000 02:56 AM     Click Here to See the Profile for nirvan sage   Click Here to Email nirvan sage     Edit/Delete Message Reply w/Quote
quote:
Originally posted by Howard Stern:
The answer is "yes". If you want any to prevent any class from being instantiated you should declare its constructor as private. Hence its constructor shall be applicable only within the class itself and not from any other class. This may apply to any class. Hope this makes it clear.

Actually it is possible to instantiate classes with only private constructors.Making constructors private only limit the way in
which we can create an instance of the class.An example

class A
{
private A()
{
System.out.println("class A instantiated");
}
public static A instantiate()
{
return new A();
}
}

class B
{
public static void main(String agrs[])
{
A a = A.instantiate();
}
}

But I think extending class A will create an error
regards nirvan

IP: Logged

maha anna
bartender
posted March 16, 2000 09:10 AM     Click Here to See the Profile for maha anna   Click Here to Email maha anna     Edit/Delete Message Reply w/Quote
The important point here is If a class has a private constructor then it is not possible to create an instance of this class from outside of the class where this private constructor is defined
We all know that any private member whether it is a var/method or for a matter of fact it is a constructor, CAN be accessed within the class itself.
So if you decide to create an instance of this class which has a private constructor through another method which has public/protected/package level accessiblity then it is fine. It is your decision. It is like tight encapsulation. Defining all member var private but giving access to any/some of them through a accessible method.
regds
maha anna

[This message has been edited by maha anna (edited March 16, 2000).]

IP: Logged

Jane Rozen
ranch hand
posted March 16, 2000 02:33 PM     Click Here to See the Profile for Jane Rozen   Click Here to Email Jane Rozen     Edit/Delete Message Reply w/Quote
Additional point:
Using private constructors gives you a way of controlling how many instances of your class could be instantiated:

code:

class A
{
static int count;

private A()
{
System.out.println("class A instantiated");
count++;
}
public static A instantiate()
{
if (count==0)
return new A();
else
System.out.println("no more A instantiated");
return null;
}
}

class B
{
public static void main(String agrs[])
{
A a = A.instantiate();
A b = A.instantiate();
A c = A.instantiate();

System.out.println("A instantiated: " + A.count);
}
}


IP: Logged

All times are MT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | JavaRanch

Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.