0

Java friends, in objectiveC with a singleton, normally you have a macro,

#define STATE [State sharedState]

Then throughout the project, anywhere at all, you just type STATE.explode() STATE.attackTanks() and so on. Similarly in Swift you can access a macro in "one word" with no further effort.

In an Android project,

public class Handy {    // singleton
 private static Handy ourInstance = new Handy();
 private Handy() {}
 public synchronized static Handy getInstance(){return ourInstance;}

 public void Example(String ss) { Log.v("Java rocks ", ss); }
}

you can access it everywhere like this

Handy.getInstance()

No problem. But my goal is to use that anywhere with "one word" only.

Of course, you can Handy HANDY = Handy.getInstance(); at the top of each file you need to use it in.

But, is there a way to make a one-word symbol which is available project-wide, with no further effort?

Sorry if the question is trivial to Java experts! Cheers


Inicdentally I believe there are other approaches (example) to singletons in Java; above I pasted in the standard code supplied by Android Studio.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Also consider the `enum` way to create a singleton: http://stackoverflow.com/q/427902/1064728 and: http://stackoverflow.com/a/71399/1064728 – Jave May 18 '14 at 13:26

4 Answers4

2

It's likely that you're thinking of the singleton aspect, only so you don't have to create instances. (And the singleton aspect is not logically significant.) In that case, just using 'static' is what you need.

If so, you are just missing the static Keyword:

public class Handy {

    public static void doSomething() {
        Log.d("Log this without instantiating the object"
    }

}

then in your class, you can just call like this:

Handy.doSomething();

(You actually use the static keyword on the method, not the class.)

Fattie
  • 27,874
  • 70
  • 431
  • 719
Booger
  • 18,579
  • 7
  • 55
  • 72
  • 1
    Now you're bypassing the whole singleton aspect though, by using `static` methods instead. – Jeroen Vannevel May 18 '14 at 13:08
  • I think the singleton aspect was just so he wouldn't have to create instances (and not logically significant). I think the 'singleton' thing is another subject, and not relevant. Think just using 'static' is what this guy needs. – Booger May 18 '14 at 13:11
  • just as Jeroen says. – Fattie Dec 24 '16 at 19:02
  • I took the liberty of editing in your excellent comment in to your answer, @Booger as it may help people (feel free to unwind of course). Cheers! – Fattie Dec 29 '16 at 11:53
2

There is no way to literally do what is asked is the question, as Java has no globals / no macros. Here is the closest similar possibility:


You can use a static method that internally uses your ourInstance variable:

public class Handy{
    private static Handy ourInstance = new Handy();
    private Handy() {}
    public synchronized static Handy getInstance(){return ourInstance;}

    public void Example(String ss){
        Log.v("Java rocks ", ss);
    }

    public static void ExampleWithoutInstance(String ss){
        ourInstance.Example(ss);
    }
}

Then you can do this from anywhere:

Handy.ExampleWithoutInstance("true");
Fattie
  • 27,874
  • 70
  • 431
  • 719
Jave
  • 31,598
  • 14
  • 77
  • 90
  • You can just create a static version of any method you don't want to call `getInstance` for. – Jave May 18 '14 at 13:25
  • @JoeBlow Except, when all the static method does is call the method of the singleton instance. – Jave Dec 28 '16 at 21:14
  • 1
    I beg your pardon, @Jave - I misread your answer. Yes, of course, exactly as you say. It's a great "trick". Thanks again for that. – Fattie Dec 29 '16 at 11:50
1

What you're looking for is basically a global variable, and Java doesn't have those


It looks like the only reason you can do it in "one word" in ObjC is because you can define a preprocessor macro that expands to the real expression, which is two words. Java doesn't have a preprocessor, so you can't do that.

What's wrong with Handy.getInstance()?

If you need to do a lot of work on the instance, it makes sense to call the getInstance() method once and store the result in a variable. You can use an instance variable within a class, or just make it a local variable within a method:

private void Stuff()
{
    final Handy handy = Handy.getInstance();

    handy.Example("yah");
    handy.AttackTanks();
    handy.MassiveExplosions();
    x = handy.bodykount;
    // etc
}

With the macro approach in ObjC, even though you only write HANDY on each line, you're actually calling the getter method every time, which is inefficient.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • Hi Wyz -- as I understand it (and tested it) you can do it perfectly in "one word", using the example I give. But you have to put that line at the "top of each file you want to use it in", so to speak. I'm wondering if there's a way to - essentially - do that globally, in some way? – Fattie May 18 '14 at 13:16
  • 1
    What you're looking for is basically a global variable, and Java doesn't have those. – Wyzard May 18 '14 at 13:19
0

I can think of one very ugly way that you should never use. Inside your singleton, add a method like this:

public Handy Handy(){
    return Handy.getInstance();
}

and use a static import on your singleton class where it's needed:

import static Handy;

Now you can use

Handy().someInstanceMethod();

You should ask yourself whether this is something you want though. Why does it matter if your code says Handy.getInstance() or just Handy()?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170