• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor with private access modifier

 
rajesh baba
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to access a constructor with private access modifier can any one explain it with a example.
i am geting a error while compiling this program
class drinks
{
private drinks()
{
System.out.println("i am abstarct constructor");
}
}
class acon
{
public static void main(String[] args)
{
drinks d=new drinks();
}
}
output:

C:\jdk1.5.0_11\bin\practice>javac acon.java
acon.java:12: drinks() has private access in drinks
drinks d=new drinks();
^
1 error

C:\jdk1.5.0_11\bin\practice>
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's private, you can't access it. (unless it's a method of the class itself which tries to instantiate itself)
 
Anita Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajesh,
You can not access private constructor from outside the class,
If you want to access the constructor from outside class,you have to make this constructor as "public".

class drinks
{
public drinks()//public constructor
{
System.out.println("i am abstarct constructor");
}
}
class PracticeTest
{
public static void main(String[] args)
{
drinks d=new drinks();
}
}

Thanks
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
try with this

class drinks
{
private drinks()
{
System.out.println("i am abstarct constructor");
}
static drinks getObject()//static method no need to create Object
{
return new drinks();
}
}
class acon
{
public static void main(String[] args)
{
System.out.println(drinks.getObject());
}
}

Thanks
Anil Kumar
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its more or less like a Singleton instance how we create.

If you decided to have any method as a private method, then that particular method *should* be called within any of the other methods of the class. This holds good for Constructor too as its a Special Method of the class!

how about this..



HtH.

Note:
-----
  • The above class is not a full-fledged, 100% singleton implementation.
  • Having a private instance of the class and checking it for null before instantiating it, is purely optional and upto the requirement.


  • [ May 28, 2007: Message edited by: Raghavan Muthu ]
     
    rajesh baba
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you friends i will try the methods
    but is there any practical application of using private constructor.i feel its not required
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    but is there any practical application of using private constructor.i feel its not required

    The Singleton pattern, where you don't want more that one instance to be created.
     
    John Stone
    Ranch Hand
    Posts: 332
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by rajesh baba:
    thank you friends i will try the methods
    but is there any practical application of using private constructor.i feel its not required


    http://en.wikipedia.org/wiki/Singleton_pattern
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Singleton is the most commonly used and the famous example.

    In some cases, where you want neither the class to have any instance methods (but static methods) nor controlling the number of objects (say, leading to singleton like patterns).

    Example, java.lang.Math class. It will be applicable for Utility classes in your application, where making an instance of utility class will really not make any sense!

    HtH.
    [ May 28, 2007: Message edited by: Raghavan Muthu ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic