Skip to main content

Get Instance not working

3 replies [Last post]
tannic2k7
Offline
Joined: 2009-07-09

I can't this getInstance() to work. This is my code.

private static final WeeklyTimerBean INSTANCE = new WeeklyTimerBean();

public static WeeklyTimerBean getInstance(){return INSTANCE;}
private WeeklyTimerBean(){}

Any info would help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
walterln
Offline
Joined: 2007-04-17

Works for me:
[code]
public class Singleton {
private static final Singleton INSTANCE = new Singleton();

public static Singleton getInstance() {
return INSTANCE;
}

private Singleton() {
}

public void hi() {
System.out.println("Hi, I am " + getClass().getSimpleName());
}

public static void main(String[] args) {
Singleton.getInstance().hi();
// for concurrency created making singleton an enum is advised now
// (besides the 'don't use singletons at all' advise :) )
SingletonEnum.getInstance().hi();
}

public enum SingletonEnum {
INSTANCE;

public static SingletonEnum getInstance() {
return INSTANCE;
}

public void hi() {
System.out.println("Hi, I am " + getClass().getSimpleName());
}
}
}
[/code]

tannic2k7
Offline
Joined: 2009-07-09

Do you have to use Singleton? Or can it be anything?

walterln
Offline
Joined: 2007-04-17

You can name the class whatever you want as long as it's a valid Java class name (or I'm not understanding you question ><).