Java: Self for static method calls within the same class

tim picture tim · Mar 27, 2014 · Viewed 11k times · Source

Is it possible to do something like self:: in PHP to not need to specify the class-name tro call a static method within the same class. See how I do it:

public class Foo
 public static void blaa() {...}
 public void foobar
 {
    Foo.blaa();
 }

but I'd like to it like

public class Foo
 public static void blaa() {...}
 public void foobar
 {
    _SOME_SORT_OF_SELF_.blaa();
 }

to not have to write down the classname over and over again... same would go for static attributes. Instead of using Foo.MY_ATTR maybe accessing it via _SOME_SORT_OF_SELF_.MY_ATTR.

Possible? Thanks

Answer

Sotirios Delimanolis picture Sotirios Delimanolis · Mar 27, 2014

If you're trying to call a static method within the class it's defined in, you don't need to specify the type. (The rules get a little more complicated with nested classes).

For instance methods and variables, you can use the this keyword in your field access and method invocation expressions.