×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

dummy question about private constructor

I'm studying Java from the very begining. I read the following defination of private constructor:

"Private- No other class can instantiate your class. You class may contain public class methods and those methods can construct an object and return it, but no other classes can."
I tried to declare a public method to construct an object in the class but was confused with the syntax and i also don't know how to call this method in other classes since new object can't be created out of the original class.
Anybody can give me an example?Thanx!
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / dummy question about private constructor
    I'm studying Java from the very begining. I read the following defination of private constructor:

    "Private- No other class can instantiate your class. You class may contain public class methods and those methods can construct an object and return it, but no other classes can."
    I tried to declare a public method to construct an object in the class but was confused with the syntax and i also don't know how to call this method in other classes since new object can't be created out of the original class.
    Anybody can give me an example?Thanx!
    • Private constructor is useful on some occasion.
      本文发表在 rolia.net 枫下论坛For example, consider such a class:

      public class House
      {
      private int floors ;

      public House(int f) { this.floors = f; }
      }

      Here, the field "floors" means the number of floors of house. And as you see, the constructor is public.

      Now consider the following situation, if someother programs create a House object in this way:

      House h1 = new House(-4) ;

      Obviously it's non-sense -- the number of floors cannot be negative. So, how to avoid such situation? This is why the private constuctor was introduced. Please see the following solution:

      public class House
      {
      private int floors ;

      private House(int f) { this.floors = f; }

      static public House createInstance(int f)
      {if (f < 1) return null ;
      House h = new House(f) ;
      return h ; }
      }

      In this case, the static public method "createInstance" does the examination and create a new object of House.

      Now if the user writes codes like this:

      House h1 = House.createInstance(4) ;
      House h2 = House.createInstance(-4) ;

      h1 will be an meaningful object and h2 is still null.

      Hope my explaination is clear.更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Thanks sailor, your explaination is very clear..
        one more question, does it mean a STATIC PUBLIC method has to be created if we declare a private constructor?
        • Yes, I think so. If not, it seems no way to create such an object ... (also another public constructor can do the job.)
          • Private constructor is related to some design patterns such as the Singleton Pattern
            Take java.lang.Runtime as an example. There is only ONE Runtime for a JVM. Everybody can get that one as follows:
            Runtime rt = System.getRuntime();

            But nobody is allowed to create a Runtime.
            ---The constructor of Runtime is private, and correspondingly, Runtime is infertile.