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.
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:
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 ]