• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Do private constructors only create 1 object? code listed

 
Ranch Hand
Posts: 246
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do private constructors only create 1 object? code listed

////////////////////////////////////////////
class Hamburger {
private Hamburger() { } // private constructor

//( only 1 object spawned ? ? )
// with public constructors infinite number of objects possible.
static Hamburger makeAHamburger(
{ // static methods do not need objects !

return new Hamburger();
}
}
public class CheeseBurger {
public static void main( String argv[]) {
// ! Hamburger x = new Hamburger();
// cannot make object directly
Hamburger x = Hamburger.makeAHamburger();
// can call static method to create object
}
}
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you make a constructor private it does not mean there will be only one object.
But making a constructor private you can make sure there is only one object at all times (Singleton class)
http://members.tripod.com/rwald/java/articles/Singleton_in_Java.html
http://www.javaranch.com/ubb/Forum33/HTML/001118.html
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but here i can make two objects....
any explanations ???
class Hamburger {
int p;
private Hamburger(int a) {p =a; }
static Hamburger makeAHamburger(int a){
return new Hamburger(a);
}
}
public class A {
public static void main( String argv[]) {
Hamburger x = Hamburger.makeAHamburger(4);// make an object x and assign p = 4
System.out.println(x.p);
Hamburger y = Hamburger.makeAHamburger(5);// creating a different object and asign p =5
System.out.println(y.p);// object y exists
System.out.println(x.p);// object x also exists
//as x.p and y.p prints their respective values does this mean that we have two objects of the class hamburger ???
// and both exist???
}
}
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Alagu ,
Please explain what you meant more in detail. I am totally confused with private constructors and static methods.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class Hamburger
{
static int p;
static private Hamburger instance = new Hamburger();
private Hamburger() { }
private static void setParam(int a)
{
p = a;
}
public static Hamburger getInstance(int a)
{
int b = a;
setParam(b);
return instance;
}
}
public class A
{
public static void main( String argv[])
{
Hamburger x = Hamburger.getInstance(5);
System.out.println(x);
Hamburger y = Hamburger.getInstance(4);

System.out.println(y);// object y exists
System.out.println(x);// no x!
/*
Hamburger@71e54aa8
Hamburger@71e54aa8
Hamburger@71e54aa8 only one reference!
Process Exit...*/

}
}
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Here's another way of doing something similar to Matthias - I'll try and explain what I'm doing.
I have a class Hamburger:

There's a few really important things about this class.
1) It has a member variable that refers to an object of the class itself. It's static and called theOneAndOnly. Because it's static, I don't need to create an instance of Hamburger for theOneAndOnly to exist. It exists from class load time and it's set to null to begin with.
2) The constructor is private. I can't call it from outside this class.
3) There's a static method called getInstance. This method is public so I can call it from outside the class and I can call it without any instance of Hamburger existing.
So now the class with my main method in. Mum provides the hamburgers but I'm only going to get one however many I ask for!

First of all I want a cheese burger. I can't call the Hamburger constructor cos it's private. So what I call is that static method of Hamburger getInstance. I can't access an object of the Hamburger class at the moment cos Mum doesn't have any instances of the Hamburger class. When I call getInstance the first time, the first thing that getInstance checks is whether or not theOneAndOnly is set to null - remember, it is automatically initialised to null. It can access theOneAndOnly because it's a static method accessing a static variable. It is set to null - we haven't actually created a Hamburger yet. So getInstance calls that private constructor to create a Hamburger - theOneAndOnly now refers to that hamburger. That line in the constructor is printed out to tell you that you have a hamburger with cheese. getInstance returns theOneAndOnly to Mum.
I push my luck and ask mum for a second burger with ketchup. Again, Mum calls getInstance. Again, we use the class name cos we don't know if there's an instance of Hamburger. But this time, when getInstance does the check to see what theOneAndOnly points to, it discovers that it already refers to that cheeseburger. So it doesn't create a new Hamburger instance. It prints out a line and then returns theOneAndOnly - still referring to the cheeseburger. So myBurger and secondBurger refer to the same burger! Mum's only ever going to get one hamburger.
I hope this makes some sense. All it's doing for me is making me hungry and it's hours to lunch.
Kathy
 
erich brant
Ranch Hand
Posts: 246
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone!
 
Your mother was a hamster and your father was a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic