Results 1 to 6 of 6

Thread: Constructors

  1. #1
    suresh.sa is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default Constructors

    Hi Friends,

    Good Morning to all...


    Can anyone tell me is it possible to have private constructors, if so what is
    the purpose of having a constructor as a private??



    Regards,
    Suresh.S.A.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You can have classes that cannot be "created" by other methods. These are used for classes with only static members.

    Additionally, you can have private classes that can only be created internally. These are called singletons. Read more on such design patterns here.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Or if you have an inner class that you want to expose to other classes, but you only want to allow it to be created inside the main class.
    eg:
    Java Code:
    public class MyMainClass {
        public class MyInnerClass {
            private MyInnerClass() {
            }
        }
    }
    MyInnerClass can only be instantiated within an object of MyMainClass.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Even if it's public, you cant instantiate it from another class... can you? I've tried this before with no success.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Even if it's public, you cant instantiate it from another class... can you? I've tried this before with no success.
    That's most likely just a syntax issue:

    Java Code:
    new MyOuterClass( ... ). new MyInnerClass( ... );
    kind regards,

    Jos

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

Similar Threads

  1. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  2. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM
  3. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •