one instance of a class

hi, i want to restrict the creation of object of a class...i want to allow to create only one object of a class .. if another object is tried to be created then error should occure,,, what should i do to achieve it plz reply me
[241 byte] By [12345789a] at [2007-11-27 8:59:53]
# 1
Have you heard of Singleton Pattern.It will allow to create only one object of a class.And in all the other places you will have to reuse the already created object.
achyuthba at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 2

This s the plass where the singleton class come into picture.

create ur class and create one method called getInstance() as below.

Make ur default constructor as private.So no class can create the object.

your_class obj = null;

public your_class getInstance(){

if(obj!=null){

return new your_class();

else

return obj;

}

Jas_rose_Discussiona at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 3

Use the singleton pattern. Your favourite search engine should be able to find the how-to.

The singleton pattern, in its basic documented form, just gives you a reference to the same object every time a new object is requested. I'm sure it can be rather easily modified to return an error the second time an instance is requested if that's what you want.

OleVVa at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 4

you don't want an error to occur just because someone tried to instantiate the class again - simply don't allow it! You're after the singleton pattern

public class MySingleton {

public static final MySingleton INSTANCE = new MySingleton();

private MySingleton() {} // prevents anyone else instantiating the class

// all your methods here

}

Your client code gets it with

MySingleton.INSTANCE;

Don't bother with the following idiom:

public class MySingleton {

private static MySingleton INSTANCE;

private MySingelton() {}

public static MySingleton getInstance() {

if ( INSTANCE == null ) {

INSTANCE = new MySingleton();

}

return INSTANCE;

}

// etc

}

It's a waste of time, doesn't achieve anything over my example (no, not even lazy loading) and is not thread-safe

georgemca at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 5

> This s the plass where the singleton class come into

> picture.

> create ur class and create one method called

> getInstance() as below.

> Make ur default constructor as private.So no class

> can create the object.

>

> your_class obj = null;

> public your_class getInstance(){

>if(obj!=null){

>return new your_class();

> else

> return obj;

Doesn't work. It'll always return null. And having the factory method in a singleton is rarely worth the effort

georgemca at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 6

> your_class obj = null;

> public your_class getInstance(){

>if(obj!=null){

>return new your_class();

> else

> return obj;

I suspect there's an error in this code so it will not allow the first object to be created. Please either check with other sources or test yourself.

OleVVa at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 7

> > your_class obj = null;

> > public your_class getInstance(){

> >if(obj!=null){

> >return new your_class();

> > else

> > return obj;

>

> I suspect there's an error in this code so it will

> not allow the first object to be created. Please

> either check with other sources or test yourself.

Not to mention that getInstance isn't static, so you'd need an instance in the first place, and there's brackets h3ll going on! Quite possibly the worst attempt at a singleton, ever!+

georgemca at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 8
It was mistaken.your_class obj = null;public your_class getInstance(){if(obj==null){return new your_class();elsereturn obj;}
Jas_rose_Discussiona at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 9

> It was mistaken.

>

> your_class obj = null;

> public your_class getInstance(){

> if(obj==null){

> return new your_class();

> else

> return obj;

> }

It's good that you want to help, but could you make sure you post working code, or you'll mislead other posters. This is still horrifically broken

georgemca at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 10
Still your constructor is not private.
achyuthba at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 11
> Still your constructor is not private.That's the least of their worries
georgemca at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 12
that is not constructor that is method . It should be public. I wrote at the first time that make ur constructor as private
Jas_rose_Discussiona at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 13
> that is not constructor that is method . It should be> public. I wrote at the first time that make ur> constructor as privateBut why have a method to return the single instance at all?
georgemca at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 14

i know about singleton pattern but what i want is that my class object's cant be created even if one tries it ...

here is my code.....

public class abc{

public static void main (String args[]){

abc s = new abc();

s.fun();

}

public void fun(){

Sin2 s1 = Sin2.s;

s1.sayHi();

[red]

Sin2 s2 = new Sin2();

s2.sayHi();

[/red]

}

}

class Sin2{

public static final Singleton s = new Singleton();

public void sayHi(){ System.out.println("Say Hi");

}

}

the line

Sin2 s2 = new Sin2();

must not be excecuted.....

its true if i made a static made or as i declared a static instance..

but if one try to create a new object privateing the custructor does nt prevent it to create oject ,,, what is the solution for this issue

12345789a at 2007-7-12 21:27:56 > top of Java-index,Java Essentials,Java Programming...
# 15

> i know about singleton pattern but what i want is

> that my class object's cant be created even if one

> tries it ...

> here is my code.....

> >

> public class abc{

> public static void main (String args[]){

> abc s = new abc();

> s.fun();

> }

> public void fun(){

> Sin2 s1 = Sin2.s;

> s1.sayHi();

> [red]

> Sin2 s2 = new Sin2();

> s2.sayHi();

> [/red]

> }

> }

>

> class Sin2{

> public static final Singleton s = new

> Singleton();

> public void sayHi(){ System.out.println("Say

> Hi");

>}

>

>

>

>

> the line

>

> Sin2 s2 = new Sin2();

>

> must not be excecuted.....

> its true if i made a static made or as i declared a

> static instance..

>

> but if one try to create a new object privateing the

> custructor does nt prevent it to create oject ,,,

> what is the solution for this issue

Yes it does. You can create multiple instances of the class from within that class, such as in main, but only there

georgemca at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 16

Here, I changed your code a bit. You did not declare the constructor for Sin2, thus a default public constructor was provided for you. To make a constructor private, you MUST declare it as private.

public class Forum

{

/**

* Creates and displays two windows of the class SecondWindow.

*/

public static void main (String args[]){

Forum s = new Forum();

s.fun();

}

public void fun(){

Sin2 s1 = Sin2.INSTANCE;

s1.sayHi();

// If you uncomment the following 2 lines, the program will not compile.

//Sin2 s2 = new Sin2();

//s2.sayHi();

}

}

class Sin2{

private Sin2(){}

public static final Sin2 INSTANCE = new Sin2();

public void sayHi()

{

System.out.println("Say Hi");

}

}

SomeoneElsea at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 17

> i know about singleton pattern but what i want is

> that my class object's cant be created even if one

> tries it ...

> here is my code.....

> ...

> the line

>

> Sin2 s2 = new Sin2();

>

> must not be excecuted.....

> its true if i made a static made or as i declared a

> static instance..

>

> but if one try to create a new object privateing the

> custructor does nt prevent it to create oject ,,,

> what is the solution for this issue

I still think you should try to modify the Singleton to suit your need.

In your code I see a reference to a class called Singleton and a declaration of a class c Sin2, but as far as I can see, neither of those are singletons as in singleton pattern.

OleVVa at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 18
in this case i dont want to have this... my class is "abc" whose "fun" method try to create an other instance of Sin2. in current scenario this is going to b true but i want to restrict it no to create other instance on use the already available instance using "Sin2.s " /......
12345789a at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 19

> in this case i dont want to have this... my class is

> "abc" whose "fun" method try to create an other

> instance of Sin2. in current scenario this is going

> to b true but i want to restrict it no to create

> other instance on use the already available instance

> using "Sin2.s " /......

Come again?

georgemca at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 20

And what about my code that I fixed for you doesnot meet this requirement? If you try to create a new object using new Sin2(), you will get a compile time error. The only way to get an instance of Sin2 is to call Sin2.INSTANCE. If you like, you can change INSTANCE to s, but I like my name more as it describes what it is better.

~Tim

SomeoneElsea at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 21

thanks for all who contributed .. i got the solution.. i was doing the right way but i missed one or the other .. thats why i couldnt get the required results. for the convinice i put all the steps here

1. create a class class Abc{}

2. create an instance as public static final Abc obj1 = new Abc();

3. make its contructor private as private Abc(){}

4. now instance of this class will be available as Abc.obj1

5. after this one cant create any other object using new keyword

thanks all

12345789a at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 22

> thanks for all who contributed .. i got the

> solution.. i was doing the right way but i missed one

> or the other .. thats why i couldnt get the required

> results. for the convinice i put all the steps here

>

> 1. create a class class Abc{}

> 2. create an instance as public static final

> Abc obj1 = new Abc();

> 3. make its contructor private as private

> Abc(){}

> 4. now instance of this class will be available as

> Abc.obj1

> 5. after this one cant create any other object using

> new keyword

>

>

> thanks all

You came up with that yourself? Independently of the multitude of people replying with just that information? Then what was the point in asking the question, if you were going to ignore the answers?

georgemca at 2007-7-21 22:51:48 > top of Java-index,Java Essentials,Java Programming...