wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor with private access modifier 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 "constructor with private access modifier" Watch "constructor with private access modifier" New topic
Author

constructor with private access modifier

rajesh baba
Greenhorn

Joined: May 19, 2007
Posts: 25
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

If it's private, you can't access it. (unless it's a method of the class itself which tries to instantiate itself)


[My Blog]
All roads lead to JavaRanch
Anita Singh
Greenhorn

Joined: May 18, 2007
Posts: 17
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


SCJP 5.0 100%
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Apr 20, 2006
Posts: 3355

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 ]

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    rajesh baba
    Greenhorn

    Joined: May 19, 2007
    Posts: 25
    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

    Joined: Nov 24, 2005
    Posts: 14688
        
      16

    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

    Joined: May 04, 2007
    Posts: 332
    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

    Joined: Apr 20, 2006
    Posts: 3355

    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: constructor with private access modifier