• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access specifiers for constructors  RSS feed

 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Suppose that there is a class A, Consider the following constructors:

public A()
{
}

private A()
{
}

protected A()
{
}

Can anybody say what is the difference among the above 3 constructors? Why we need put access specifiers to constructors?

Thanks in advance...

Shashi
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You forgot that constructors can be of default access too.

Well they work exactly as they do for methods. As to why you would want to use them.

You can use private constructors to help you create singletons. Singletons are class where you only require or want one instance to exist at any one time. You would usual provide a static public "initialisation" method which is able to construct the class and return it.

Something like this:


In a multi thread inveronment it is possible (using teh above code) to end up with one then a single instance.

Using default and protected you can allow only package members to create instances of the class. Protected can also be used to create singletons.

I am sure others can give a better explaination, I guess its down to your intened use for teh class and where you would like it to be constructed.
[ February 22, 2008: Message edited by: Gavin Tranter ]
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Gavin,

As you say Protected can be used to create Singleton classes, how it can be? Can you please explain more detail? And one more doubt..why you did specify Singletone constructor as private. what happens if I wrote as follows:

protected Singleton
{
}

Help please..
Shashi
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Java has four (4) levels of access to class members, private, default (this is when no modifer is supplied) protected and public.

Private is the must restrictive and public the must open.

For a deeper explanation of accessors see HERE

Ok, A singleton is a design pattern, the pattern calls for only one instance of a class to exist during the execution of the application.

If a classes constructor is public it can be called in the normal ways and you can create as many instances of a given class as you wish.

If the constructor is private, then no instance of the class can be created from outside the class, because the compilor/JRE can not see that constructor. Remember private access means that only the class itself can see the member marked as private, and classes outside can not see a private memeber.

Now while you can use protected and default to provide a singleton, these accessors still allow classes in the same package and inherited classes to call teh constructor, so the to ensure total control the best is to use private.

I have explained teh code a bit this time:


Ok thats the singleton, the only way to get an instance is to call EgSingleton.getInstance();



The code above when ran should produce there lines of out put, the first saying tthe singleton was being created and then two lines with a nd b both being equal to teh same value. The value is the memory address. If you were to make the EgSingleton constructor protected or default you should get the same results.

If you made the constructor public replace the lines with getInstance on them with the following and you will see that two different objects are created.

 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gavin,

Thanks for more explanation..!! I understood good.

-Shashi
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!