File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes public constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "public constructor" Watch "public constructor" New topic
Author

public constructor

sampath kumar
Greenhorn

Joined: Sep 22, 2006
Posts: 6
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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 ]

all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public constructor