Hi ..
How to access private constructor from outside class..
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi ..
How to access private constructor from outside class..
The simple answer is you don't. If you have access to the source of the class that has the private constructor, there are 3 things you can do:
change the constructor in question to public. Now every class has access to that constructor.
change the constructor to protected. Now all inheriting classes have access to that constructor. Furthermore, inheritance allows for the inheriting class to "expand" the access to public (note that it can't restrict the access).
Create a "factory constructor". This is a static method that makes sure all the input is valid and then creates the object via the private constructor. Once the object is created, it returns the reference/handle to that object.
If you don't have access to the source (or aren't allowed to change it), there's nothing you can do to access that constructor directly (note, there might be some other methods that will use that constructor, somewhat similar to the factory constructor).
ahmmm im not yet familiar with private, public, protected modifiers,,
but i want to ask this in advance for my study...
you said that "protected" methods/constructos, can be accessed by inherited classes.. how bout private? can it be accessed by inherited ones?
in private , only the class itself can access those methods what so ever...
Last edited by chronoz13; November 7th, 2009 at 03:39 AM.
No, private cannot be accessed by inheriting classes.
chronoz13 (November 8th, 2009)
There are 4 access modifiers that you need to know about. Default, private, protected and public.
Default
This is accessible by all classes in the same package.
Private
This is accessible by anything inside the same class.
Protected
This is accessible by anything inheriting the class in question.
Public
This is accessible by any class in any package.
The ones that might be somewhat of a problem understanding is the default and protected modifiers. Private and public are just what they say, private means no one else has access, public means everyone has access.
For more information have a look at Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
An example use case of a private constructor can be when you create a class as a singleton or a single instance using a getInstance method.
public class MySingleton { private static MySingletone instance = new MySingleton("This takes a string");; private MySingleton(final String myString) { // This is a private constructor } public static MySingleton getInstance() { return instance. } }
In the above class we've effectively removed any ability for another class to instantiate this class as there is no public constructor but instead you can call the getInstance method to get an instance of it. We then use the private constructor to instantiate this class as a private static variable.
// Json
Last edited by Json; November 7th, 2009 at 04:11 PM.
chronoz13 (November 8th, 2009)