• Post Reply Bookmark Topic Watch Topic
  • New Topic

This Static Method Cannot Be Used Directly. Why?  RSS feed

 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I noticed that the max method of Math class cannot be used directly in main function. But, max method works fine with className. I know that the static methods can be used as both className.methodName() and just methodName(), cannot it?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

If you want to use methodName only, you have to specify that with a static import.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you want to use methodName only, you have to specify that with a static import.

@Hasan: And I'd advise against that, because it makes the static method "look like" an instance one.

Is there any particular reason you don't want to type "Math."? It's only five characters, and it adds a lot of clarity to your code.

Winston
 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No.

If you want to use methodName only, you have to specify that with a static import.


The answer which I searched was that. Thank you so much.


Winston Gutkowski wrote:
Campbell Ritchie wrote:If you want to use methodName only, you have to specify that with a static import.

@Hasan: And I'd advise against that, because it makes the static method "look like" an instance one.

Is there any particular reason you don't want to type "Math."? It's only five characters, and it adds a lot of clarity to your code.

Winston


Yes, there is. The reason is just learning purpose.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasan Fatih wrote:Yes, there is. The reason is just learning purpose.

Then I suspect you have your answer. I still advise against it though.

Good luck.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!