|
UBBFriend: Email This Page to Someone! |
JavaRanch
Big Moose Saloon Programmer Certification ? on private constructors ........
|
next newest topic | next oldest topic |
Author | Topic: ? on private constructors ........ |
eram ranch hand |
posted March 14, 2000 05:03 PM
Hi all, I am new to this Ship. found great! Can a constructor be declared private ? 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. IP: Logged |
Howard
Stern ranch hand |
posted March 14, 2000 05:12 PM
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
Thanks .I got it. IP: Logged |
nirvan
sage greenhorn |
posted March 16, 2000 02:56 AM
quote: Actually it is possible to instantiate classes with only private
constructors.Making constructors private only limit the way in class A class B But I think extending class A will create an error IP: Logged |
maha
anna bartender |
posted March 16, 2000 09:10 AM
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
Additional point: Using private constructors gives you a way of controlling how many instances of your class could be instantiated:
code: IP: Logged |
All times are MT (US) |
next newest topic | next oldest topic |
|
Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation
(formerly Madrona Park, Inc.), 1998 -
2000.