Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Self for static method calls within the same class

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

like image 576
tim Avatar asked Mar 27 '14 22:03

tim


1 Answers

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.

like image 150
Sotirios Delimanolis Avatar answered Sep 18 '22 12:09

Sotirios Delimanolis