I don't understand why a static method can't use non-static data. Can anybody explain what the problems are and why we can't do it?
|
In most OO languages, when you define a method inside a class, it becomes an Instance Method. When you create a new instance of that class, via the Static Methods, by contrast, are ignorant of individual class instances. The static method is similar to a free function in C or C++. It isn't tied to a specific instantiation of the class. This is why they cannot access instance values. There's no instance to take a value from! Static Data is similar to a static method. A value that is declared A Static Method can access Static Data because they both exist independently of specific instances of a class. It might help to look at how you invoke a static method, compared to a instance method. Let's say we had the following class (using Java-like pseudocode):
UpdateAs COME FROM points out in the comments, a static method is capable of working with non-static data, but it must be passed explicitly. Let's assume the
|
|||||||||||||||||||||
|
Lets explain it with a hypothetical sample. Imagine a simple class:
Now we create 2 instances of this class:
now, think - what if we add a new static method to User, eg:
and you call it:
what would x contain? "Jim", "Bones", or something else? The problem is that a static method is a single method, defined on the class, not the objects. As a result, you don't know which object it might apply to. This is why its a special thing. Its best to think of static methods as individual things, like functions in C for example. That languages like Java have them contained inside classes is mainly a problem with Java not allowing anything to exist outside a class, so functions like this have to be forced inside a class in some manner (a bit like how main() is forced to be inside a class too when all sense says it should be a singular, standalone function). |
||||
|
Non-static data is associated to an instance of the class. Static methods (and data) are not associated to a particular instance of the class. There does not need to be an instance of a class to use static methods on it. Even if there were instance(s), there would be no way for Java to guarantee that you are operating on the instance you are expecting when you call a static method. Therefore, static methods cannot have access to non-static data. |
|||
|
It can use field data; consider the following java code:
|
|||||||||||||||||
|
Think of it as static methods living in a non-object-oriented dimension. In the "object oriented dimension" a class can spawn multiples egos (instances), each ego has conscience of itself via its state. In the flat, non-OO-dimension a class is oblivious of their egos living in the OO-dimension. Their world is flat and procedural, almost as if OOP had not been invented yet, and as if the class was a small procedural program, and the static data were just global variables. |
|||
|
I think the easiest way to explain this is to look at some code and then consider what results we would expect the code to produce.
For completeness here is the car class:
|
|||||||||
|
The other answers pretty much say it all, however, there is some "detail" I'd like to add. Static methods (say those in Java) just don't have an implicit object associated to them (accessible through That doesn't mean they cannot access non-static data.
I know this is just a detail, but I found your question strange when I read it. "Can use only static data" is too much restrictive. By the way, I didn't test the code, I just wrote it here in order to exemplify what I was saying. |
||||
|
I think the issue here is one of understanding. From a technical standpoint a static method called from within an object would be quite capable of seeing the instance fields. I strongly suspect this is what caused the question in the first place. The issue is that methods can be called from outside the object. At that point there's no instance data to provide them--and thus no way for the compiler to resolve the code. Since allowing instance data caused a contradiction we must not allow instance data. |
|||
|