-2

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.

7
  • 2
    So 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? Commented Jan 15, 2014 at 19:18
  • 2
    Using this.Func() certainly isn't easier to type than just Func(). Commented Jan 15, 2014 at 19:18
  • possible dup of stackoverflow.com/questions/12174573/… Commented Jan 15, 2014 at 19:19
  • You can't call that method with this in a static context.. Commented Jan 15, 2014 at 19:20
  • Possible duplicate of stackoverflow.com/questions/7884004/… Commented Jan 15, 2014 at 19:25

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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/… –

1 Comment

@SecureFish is this makes sense to you my friend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.