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.

Results 1 to 5 of 5

Thread: Private Constructor

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Private Constructor

    Hi ..


    How to access private constructor from outside class..


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Private Constructor

    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).

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Private 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.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Private Constructor

    No, private cannot be accessed by inheriting classes.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (November 8th, 2009)

  6. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Private Constructor

    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.

  7. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (November 8th, 2009)

Similar Threads

  1. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  2. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM
  3. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  4. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM