One static method Func() is defined within a class. Invoking it in this.Func does not have compile error. Also, invoking static method against an object doesnt have any disadvantage. And make calling method simple since the class name might be difficult to spell. Not sure, this is a good java coding way.
Asked
Active
Viewed 185 times
-2
-
2So you want to use `this.Func()` just because you don't have to spell class name? Great. How would you call the method from outside the class then? – Rohit Jain Jan 15 '14 at 19:18
-
2Using `this.Func()` certainly isn't easier to type than just `Func()`. – Mark Peters Jan 15 '14 at 19:18
-
possible dup of http://stackoverflow.com/questions/12174573/java-convention-on-reference-to-methods-and-variables – JustinKSU Jan 15 '14 at 19:19
-
You can't call that method with `this` in a static context.. – nachokk Jan 15 '14 at 19:20
-
Possible duplicate of http://stackoverflow.com/questions/7884004/is-calling-static-methods-via-an-object-bad-form-why – Mark Peters Jan 15 '14 at 19:25
-
This is a really bad java coding way. – thiago.lenz Jan 15 '14 at 19:27
-
See also: http://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co – Mark Peters Jan 15 '14 at 19:28
2 Answers
3
If you are calling a static method, you shouldn't use an instance, even though it compiles because it is plain confusing. Consider the following.
Thread t = new Thread( ... );
t.start();
t.sleep(1000);
The last method doesn't operate on the thread t
as it is static. It causes the current thread to sleep.
Thread t = null;
t.yield(); // compiles and runs even thought `t` is null.

chrylis -cautiouslyoptimistic-
- 75,269
- 21
- 115
- 152

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
0
It is better to use this with class name to maintain the readability because if someone wants to debug in your code, so he has not to go back and see your variable declaration that it is marked as static or not, so it is better to use with class name so no need to go back and think about it
ya it is the possible duplicate of stackoverflow.com/questions/7884004/… –