The Artima Developer Community
Sponsored Link

Java Answers Forum
Class with private constructor

5 replies on 1 page. Most recent reply: Mar 15, 2004 5:09 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Walter Rieppi

Posts: 5
Nickname: walter2004
Registered: Mar, 2004

Class with private constructor Posted: Mar 11, 2004 12:16 AM
Reply to this message Reply
Advertisement
Hi people,
i need to resolve thi question. I have this class
Class car()
{
private car ()
{
....
.....
}
}

In main program i need to create an car object. I can't use that constructor because its private. There is anther way to create an object of that class or not ????
Thanks Walter


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Class with private constructor Posted: Mar 11, 2004 9:26 AM
Reply to this message Reply
I'm guessing something like the Factory Design Pattern?

public class car() {
  private car () {
    // do something
  }
 
  public car getInstance() {
    return( new car() );
  }
}
 


Adam

David Ramsey

Posts: 34
Nickname: dlramsey
Registered: Apr, 2002

Re: Class with private constructor Posted: Mar 11, 2004 2:06 PM
Reply to this message Reply
Minor correction, since the factory method has to be static to get an instance otherwise it needs an instance to be called first:

public class car() {
  private car () {
    // do something
  }
 
  public static car getInstance() {
    return( new car() );
  }
}

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Class with private constructor Posted: Mar 11, 2004 8:47 PM
Reply to this message Reply
Since you are planning to invoke the class constructor from the main of the same class you can easily do that.

Walter Rieppi

Posts: 5
Nickname: walter2004
Registered: Mar, 2004

Re: Class with private constructor Posted: Mar 15, 2004 12:24 AM
Reply to this message Reply
Thanks all. I tried both solution with my Eclipse. Why i have an compilation error if the class Sundae makeSundae is not a static class???

public class Sundae
{
private Sundae()
{}

public static Sundae makeSundae()
{
return (new Sundae()) ;
}
}

Since, i can create an object only if the makeSundae is a static. Anyone can me explain this ???
Best Regards Walter

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Class with private constructor Posted: Mar 15, 2004 5:09 AM
Reply to this message Reply
> Thanks all. I tried both solution with my Eclipse. Why i
> have an compilation error if the class Sundae makeSundae
> is not a static class???

I think David has already answered this, above. If a method is not static then it can only be called via an instance of the class. For example:
Sundae s2 = s1.makeSundae();
This means that you need an existing instance to create a new instance. That's OK except that you won't be able to create a first instance to start things off (because the constructor is private). Therefore, the makeSundae method needs to be static so that you can write the following:
Sundae s = Sundae.makeSundae();


Vince.

Flat View: This topic has 5 replies on 1 page
Topic: Diff  b/w a API server and a Web server Previous Topic   Next Topic Topic: exercise 11 Bruce Eckel chapter 4

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use