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;
}
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.
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
> 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
> 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.
> > 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!+
> 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
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
> 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
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");
}
}
> 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.
> 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?
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
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
> 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?