Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
Sam Cooper
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the constructors be marked private or protected or friendly (none). If so whats their significance.
Thanks
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The access modifiers on constructors will work the same as when they are used with methods.
If you mark a constructor private, only methods within the class itself can use that constructor. That means code outside this class cannot instantiate any objects! Although this might seem useless, it is actually a very-well used technique when using the Singleton design pattern.
If you mark a constructor protected, then only subclasses of the class, or classes in the same package, can call the constructor. This is useful in situations where you may not want an outside method to instantiate an object of that class, but you want to be able to subclass the object, and construct a sublclass. The subclass now has access to the superclass constructor and can instantiate it. This is actually used in the io package - the no-arg constructor of ObjectOutputStream is marked protected, so you can't instantiate an ObjectOutputStream via that constructor. But you can create a subclass of it, and then call the superclass no-arg constructor.
Finally, default access is similar to protected above, but only methods in the same package as the object can use the constructor to instantiate a class of that type.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes they can.
the meaning is the same as for methods.
if a constructor is private that means that only the method within the class can call it, and thus, create an instance of the class. This is usually used to build so called Factory classes which have a static method like getInstance() or something that return an object of the class. That way you can control how many instances of your classes are created.
If it is default, then it may only be called by classes within the same package.
If it is protected, then it may only be called by classes within the same package and subclasses in other packages...
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read somewhere that constructor can only be defined as public or protected, but when I tried to define a private constructor, it works!
I guess all the explanations below solved my query.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
choi,
when in doubt read the JLS... and the latter says in 8.8.3 Constructor Modifiers

ConstructorModifier: one of
public protected private
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic