jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor doubt" Watch "constructor doubt" New topic
Author

constructor doubt

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi in k&b

regarding constructor its written ..

Constructors can use any access modifier, including private. (A private
constructor means only code within the class itself can instantiate an object of
that type, so if the private-constructor class wants to allow an instance of the
class to be used, the class must provide a static method or variable that allows
access to an instance created from within the class.)


i m not able to get what it means ...?


Thanks and Regards, Amit Taneja
Glen Fernandes
Greenhorn

Joined: Jan 28, 2005
Posts: 5
Hello Amit,

It means that the only place you can use the operator/keyword new to instantiate an object of the class (with a private constructor) is in a method inside that class.

However it can't be a non-static method, since that requires an object that is already instantiated with new. So a static method is needed to return a constructed object.

If a code example helps:


You can read up on the Singleton design pattern which relates to this.

Regards,
Glen
Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
oh yes, it gives stackoverflow error
Glen Kidston
Greenhorn

Joined: Feb 26, 2005
Posts: 9
Kalyani,
I see nothing in Glen (Fernandes') code which would cause a stack overflow. I am not sure what you mean?
-Glen (Kidston)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
ok thanx for your reply
but tell me why it can't be non-static method ?

in static it works fine as u show us...but what if i try to instantiate a object from a non-static method that is in same class ?

what is Singleton ?

and why u ppl are talking about stack overflow ?

do reply..
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Originally posted by amit taneja:
ok thanx for your reply
but tell me why it can't be non-static method ?

To clarify - it's not that an instance method (non-static method) couldn't instantiate an object of the class - it's that it wouldn't do any good to the outside world if that were the only place in the class where an object of that type was instantiated.

Say we're talking about a class named YouCantMakeMe, with a private constructor. Think of some code in another class... in order to get to use any of the functionality of the YouCantMakeMe class, it would need an instance of that class. If the constructor is private, only a YouCantMakeMe object can call the constructor - this other class can't simply use "new YouCantMakeMe()". It has to depend on something within the YouCantMakeMe class to do the instantiation.... but now, what methods in the YouCantMakeMe class can be called without a YouCantMakeMe instance? Only static methods (that is, the ones you call by referencing the class, not an object instance of the class).

It's a chicken-and-the-egg problem... I can't get an instance until I call this instance method - but I can't call this instance method until I have an instance...


in static it works fine as u show us...but what if i try to instantiate a object from a non-static method that is in same class ?


That should work fine.... and so long as there is also a static method that gives access to an instance upon which you can CALL that instance method, you'll be able to use it. So you might have to have:





what is Singleton ?

Singleton is a common design pattern in which you want only one instance of a particular class to exist in your program (the entire JVM). It is also generally globally available, meaning you can get the same (only) instance from just about anywhere in your code, so you can think of it as a global variable. To accomplish it, you do something like the following:






and why u ppl are talking about stack overflow ?

I can only guess that Kalyani was thinking of a constructor which instantiated an object of it's class, causing recursive calls to the constructor method. Something like:




do reply..


Hope that helps.

-- Jon
Francisco Gonzalez
Greenhorn

Joined: Apr 11, 2005
Posts: 15
Amit,

Singleton it is a design pattern that allows you restrict a class to have only 1 instance.

So in order for you to accomplish this you need to declare the constructor with a private access modifier, so nobody will be able to do a new on the class. But at the same time you will need to provide an static method that can be called and that return that only instance. Why an static method ? Well think a about it, if you cannot instantiate the class in the first place, how are you going to call a instance member ?

Look at this example:



With a class following that pattern nobody outside the class will be able to do a new Singleton() but instead needs to use the static method in order to get an instance.

Regards,
Francisco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor doubt