• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public constructor

 
sampath kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Consider this class

package PackA;
class Sample
{
public Sample()
{

}
}
Note that the class is having default package visibility.
Then What is the idea behind the public constructor when the class itself is not visible outside the package?
Can anybody any uasge or neccessity of above public constructor?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the classes within the package can instantiate this class. If the constructor were private even classes within the default package cant instantiate this class.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sampath kumar wrote

What is the idea behind the public constructor when the class itself is not visible outside the package?
Can anybody any uasge or neccessity of above public constructor?


There is no usage of a public constructor in a default class. And there is no idea behind it. It's just bad design. The constructor should be default as well, but public doesn't do any harm, just puzzles people.
The only situation where you put an access level to a constructor is when you mark it private to hinder other classes to instantiate it.


Or am I wrong?

Bu.
[ September 30, 2006: Message edited by: Burkhard Hassel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic