my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Accessing a static method in a non static way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing a static method in a non static way" Watch "Accessing a static method in a non static way" New topic
Author

Accessing a static method in a non static way

Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Why does my editor (WSAD) care that I instantiate class SomeClass into object foo before I call the static method doSomething()?



The warning that I get is the following:
"the static method doSomething() from the type SomeClass should be accessed in a static way"

I realize that the doSomething() method is a static/class method and that I can call it without instantiating the class (e.g. SomeClass.doSomething() ), but it works with foo.doSomething()

I guess my question is why does the editor want me to use Class.doSomething() versus foo.doSomething() and should I listen to it?

**NOTE the static method doSomething() is a setter for an instance variable.
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
It is generally considered bad practice to access static functions/variables using instances of the class rather than calling them explicitly.

Consider someone reading an extension of this code



Now if someone references this class as follows:


If this updateVar() method were not static (and therefore could not update a static variable) someone looking at this code would assume that there was two instaces of the class Blah being created each with its internal "var" set to one. However since they reference a static variable var (at the end of execution) holds the value two.



It is considered better practice to access static functions/variables in this way as a casual glance at the code will easily show the functionality.

So to answer your question the IDE is not giving out to you for creating an instance of the class that holds static methods .. it's giving out at the way in which these static methods are called.

Hope this helps,
Barry
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Chris Staten:
Why does my editor (WSAD) care that I instantiate class SomeClass into object foo before I call the static method doSomething()?



The warning that I get is the following:
"the static method doSomething() from the type SomeClass should be accessed in a static way"

I realize that the doSomething() method is a static/class method and that I can call it without instantiating the class (e.g. SomeClass.doSomething() ), but it works with foo.doSomething()

I guess my question is why does the editor want me to use Class.doSomething() versus foo.doSomething() and should I listen to it?

**NOTE the static method doSomething() is a setter for an instance variable.

You don't need to create an object before calling a static method. And from your last comment, I dont' understand why this method is static. You can't directly access instance variables in a static method. If doSomething() really is a setter for an instance variable, it shouldn't be static.

As stated above, you should call static methods with the class name, not an object of the class. In other words, if doSomething is static you should do something like this:


Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Sorry Layne, I didn't proof read my post well enough. This static method is setting a STATIC variable. I didn't mean to say that it was setting an instance variable. I didn't mean to confuse anyone, it's just that sometimes my fingers get excited and type something other than what my brain is thinking.

Barry, thanks for the reply. I didn't write the original code so I'm not sure what the reasoning was for writing it this way.
[ December 16, 2004: Message edited by: Chris Staten ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a static method in a non static way