views:

93

answers:

4

I know that is a beginner's question. I'm new to java and and also to programming in general.

Say I got a class that has only static data, example:

class Foo {
private static int x;  }

I want to use the class without instantiating any object. So I want to be able to do:

Foo.setX(5);
Foo.getX();

What is the best way to implement this class, I'm a little bit confused about interfaces and other stuff for now.

Thanks.

+3  A: 

I think a Singleton is what your looking for:

The singleton class:

public class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    private int x;

    // Private constructor prevents instantiation from other classes
    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

}

And to access from anywhere in your code :

int x = Singleton.getInstance().getX();
Singleton.getInstance().setX(10);
Alois Cochard
["Every time you make a singleton, God kills a start-up, two if you _think_ you’ve made it thread-safe."](http://www.dadhacker.com/blog/?p=1274)
sbi
@sbi 100% agree, singletons are evil !
Alois Cochard
If it is evil, is there a better design? Or should I change my design of solving my problem?
m4design
Would be good to see the whole code to give you advice. But I feel that dependency injection would prevent you using singleton ... just a feeling ;-) http://en.wikipedia.org/wiki/Dependency_injection
Alois Cochard
BTW, this can be a whole different question if you want detailed answers on SO ...
Alois Cochard
@sbi: lovely quote, though in Java it's trivial to make a Singleton threadsafe - there's just this utterly pointless "super-lazy evaluation" meme that people keep dredging up.
Michael Borgwardt
@Michal Borgwardt ... even if thread safe, that's not good for Unit Testing, and can be painful when refactoring.
Alois Cochard
@Michael: So it's just one startup. Wow. That feels so much better.
sbi
@m4design: Basically, a singleton is just a glorified global variable. And, basically, global variables just mean that developers were to lazy to properly hand around local variables.
sbi
+3  A: 

Why don't you just define two static methods that modify/return the static field?

Static Methods in Java

testalino
Because I said before: "I want to use the class without instantiating any object"
m4design
When you define a static method, you don't have to instantiate the class to access the method. You just write Foo.GetX().
testalino
@m4design: I edited my answer. Please read this article to get an overview.
testalino
A: 

You can always have static public methods Get and Set to do what you want. But have a look again, Singleton Pattern may be what you want.

sheki
+1  A: 

You can just define getX and setX methods as static:

class Foo
{
    private static int x; 

    public static int getX()
    {
        return x;
    }

    public static void setX(int x)
    {
        Foo.x = x;
    }
}

Then you can use this without instantiating any object:

Foo.setX(5);
int val = Foo.getX();

As others have suggested, a Singleton is a cleaner approach, although it will not meet your requirement of not instantiating any object.

http://en.wikipedia.org/wiki/Singleton_pattern

Grodriguez
I think this this may work in C++ but not in Java (if someone can conform). I tried this in Java and it tells me that (non-static cannot be referenced from a static context). even thought x is actually static !!
m4design
That work in Java, just try ! you will see ! ... btw I recommend singleton against, think twice before impl., and are you sure you need static at all ?
Alois Cochard
There is a fault. this.x = x; won't work. It has to be Foo.x = x;
tkr
But if you need state, you should really create an object. What is wrong with Foo foo = new Foo(); foo.setX(1);
tkr
@tkr: You are correct, it's fixed now. Thanks!
Grodriguez
@m4design: Try now. It should work.
Grodriguez