• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor with private access modifier

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    There's a city wid manhunt for this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic