Java: a reference to a static context similar to the self-reference `this` class, the frequent use of` this` as an encoding style

Brought up in python since I recently started Java, I constantly used the self-consistency of the this class regardless of whether it is mandatory. I believe that this helps the readability of the code, because the people who read it should not independently determine which area the variable belongs to. I would like to work in a similar way to the static domain: before all references to the static context with the static and, thus, make the code more understandable. Java, however, is not pleasant. I know that I can technically just use the class name, but it looks pretty ugly, especially when the class has a long name. I could also precede the names of static methods and attributes with a special prefix, but I would not want to be seen as paranoid. Do you guys think this is a good OOP style? Or should I just completely forget about something above and let javac do all the scope binding?

+7
source share
5 answers

There are some conventions that prefix all uses of member fields (or even methods) with this . There are also conventions for prefixing variables or class names (the notorious Hungarian notation). For example, official Android code style rules require a prefix to the name of a private member name using m- (e.g. private int mSize ) and private static with s- .

Personally, I hate both of these conventions because they simply clutter up the already verbose Java code. Modern IDEs provide some means of visual differentiation. For example, in Eclipse, local variables are in a standard black font, while fields are blue. Static methods or fields are in italics.

They also provide great ways to navigate the source code. Ctrl + click on the variable name / method / type allows you to go to its declaration. There are common views. Eclipse even displays a tooltip with information about such a thing only if you hover over it.

Yes, someone may argue that sometimes you do not read the code in the IDE, but in a simple text editor. I sometimes do this to quickly view the code, but to be honest, whenever I want to do some serious encoding without using the IDE, it's just masochism.

+3
source

+1 to use the term sup! But -1 for trying to apply pythonic idioms to Java :) Most Java programmers expect to see the this only where there is uncertainty that needs to be resolved. Using it liberally, as you suggest, is likely to make others overly suspicious. For statics, I think you can use some Hungarian notation if it is your bag, but it is also not so difficult to define it when looking at the code (unless it is terribly monolithic or something else).

+3
source

Do not try this at work:

 public class Foo { private static class This extends Foo{} static int x; static void f(int x){} void test() { This.f(This.x); } } 
+3
source

Most IDEs can very quickly find the source of the referenced variable. Therefore, it is currently considered simply distracting to add additional hints regarding their scope, etc.

In short, please do not use this unnecessarily, and please give up all your efforts to keep the hands of the programmers who come after you, except, obviously, choosing good names, writing good code, and adding good comments.

+2
source

You can put all static methods in a static inner class with a simple naming scheme. For example:

 public class Myclass { public void instanceMethod() { Static.doThing(); } public static class Static { public static void doThing() { //blah } } } 
+1
source

All Articles