0

I'm trying to implement a singleton pattern for a System class. The examples I found do not compile (e.g. http://www.tutorialspoint.com/java/java_using_singleton.htm). There is a static method in a non-static class. So I made the class static and all was well until I tried to make a member variable for my Timer class.

Now I get the message "No enclosing instance of type scene_3d is accessible. Must qualify the allocation with an enclosing instance...

I have searched around but nobody's singleton patterns compile for me. By the way I am using Processing (a Java IDE/extension). Any ideas on how to fix this will be of great help. Thanks!

static public class DemoSystem {
  private static DemoSystem instance = null;  
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    Timer timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
}
ballaw
  • 1,489
  • 6
  • 20
  • 28
  • possible duplicate of [Implementing the singleton pattern in Java](http://stackoverflow.com/questions/2008912/implementing-the-singleton-pattern-in-java) – Brian Roach Feb 13 '13 at 06:25

4 Answers4

2

The standard singleton pattern is to have a private constructor and a static instance variable, so:

public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
}

Once you fix your imports, this should work fine.

hd1
  • 33,938
  • 5
  • 80
  • 91
1

timer is declared inside init. It has to be up in the class so the get method can access it, e.g.:

static public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  public void init() {
    timer = new Timer();  
  }

  public int getTime() {
    return timer.elapsedTime;
  }
}

You were also missing the public qualifiers on the two methods.

Rob
  • 11,446
  • 7
  • 39
  • 57
1

I don't think you can declare your entire class as static:

static public class DemoSystem {

should be:

public class DemoSystem {

also, and no 100%, may have a syntax error (you are missing a semicolon...):

   protected DemoSystem() {}

should be

   protected DemoSystem() {};
Tucker
  • 7,017
  • 9
  • 37
  • 55
1

There are some points related to the origin code snippet posted.

  1. First of all the top level class can not be declared as static.

  2. The public method you have created ( public static DemoSystem Inst() ) won't take guarantee that it will create singleton object only in case of concurrency. For that initialize the object at the time of declaration only like : private static DemoSystem instance = new DemoSystem();

or synchronize the method properly.

  1. Make Timer timer , a class level variable .

  2. What is timer.elapsedTime . I don't think there is any such property in Timer class. If you are using different api , make necessary imports.

So the final code will look like this :

import java.util.Timer;

public class DemoSystem {
    // Declared the variable at class level.
    Timer timer = null;

    // Initializing the object here only.
    private static DemoSystem instance = new DemoSystem();

    // Made the constructer private.
    private DemoSystem() {
    }

    // Static method to get singleton instance
    public static DemoSystem Inst() {
        return instance;
    }

    void init() {
        timer = new Timer();
    }

    int getTime() {
        //commented the statement so to compile the class  
        //return timer.elapsedTime;
        return 1;
    }
}
Java World
  • 71
  • 1
  • This fails to compile. I get the error "The field instance cannot be declared static; static fields can only be declared in static or top level types." Do you think this is a limitation of Processing? I'm using my own Timer class by the way. – ballaw Feb 14 '13 at 01:55
  • Hi , I think this might be a limitation with Processing because i have successfully compiled the same code in eclipse juno. – Java World Feb 14 '13 at 12:42
  • Well are u able to compile and run your code now. If not pls tell which IDE and Processing plugin are u using for the development. – Java World Feb 14 '13 at 12:42