• 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 in private

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I read in some book that "Math constructor is private, cannot be instantiated." Is this correct? If costructor is in private section, can't we create an instance? I tried this, its working fine. If costroctor is in private section, its not possible to Extend that class. Any information on this....
regards,
sri.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a constructor in a class definition is private, then you cannot create an instance of that class using that constructor outside of the class definition. If a class is declared final, then you can't extend it.
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that is the case just check this code. Here constructor is in Private, and i am creating an object with that costructor.

public class Temp
{
private Temp()
{
System.out.println("Hello Rallapalli");
}
public void f()
{
int a=10;
System.out.println(a);
}
public static void main(String [] args)
{
Temp t = new Temp();
t.f();
}
}

According to ur explanation, it should not create an instance. But it is creating and working fine.
Please let me know.
regards,
sri.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sri rallapalli:

According to ur explanation, it should not create an instance. But it is creating and working fine.
Please let me know.
regards,
sri.



Keith explanation is correct, but I'll give it another try.

A private constructor means that it is private. It can only be used by that class. For example, only code in the Math class can instantiate a Math object, because it is private to the Math class.

Anyway, try to instantiate a Temp object from another class -- you should not be able to.

Henry
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in addition to Henry's reply.

His explanation is valid for private members as well. So if you try to create an instance of the same class inside the same class you will be able to access its private members. On the other hand, if you try to create an instance for that class inside another class, for sure you will not be able to access its private members.
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to remind that this kind of pattern is often used in J2EE called Singleton Pattern.


Regards,
Narendranath
 
Ranch Hand
Posts: 115
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, its true...

Private constructor will not allow to instantiate from another class...
but it will allow from within its class...

I learned one more point...

Thank u...

Devisri.
 
reply
    Bookmark Topic Watch Topic
  • New Topic