• 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 doubt

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yes, it gives stackoverflow error
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anything worth doing well is worth doing poorly first. Just look at 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