8

Sup guys, I have a simple, but bugging question. As far as I understand, static basically means, that for every single instance of that class, this method will be the same, if we change it, this will change for every single instance of that class, it's also known as Class Method. Well, if I have a class that implements toString () method witch a certain format, let's say:

public String toString() {  
    return "(" + x + "," + y + ")";
}

Why can't it be set as static? Since this format will be the same for every single instance of that class...?

Chris Dobkowski
  • 325
  • 4
  • 13
  • 6
    Think about x and y. If they are non-static fields, a static method cannot access them. – Patricia Shanahan Dec 16 '13 at 01:02
  • @PatriciaShanahan so you say that if the x and y were static values, the toString() method could be set as static? – Chris Dobkowski Dec 16 '13 at 01:04
  • Your understanding of `static` is slightly off. `static` means that only one instance is created (at least for variables). You can access `static` variables / methods without even instantiating an object (eg `ClassName.toString()`. Also, you don't have to make a new method for each object; they all have the same method's available – Justin Dec 16 '13 at 01:05
  • No, because in class Object, toString() is not static. If you wanted, you could make a static method for it, but things like System.out.println wouldn't call it. – Vitruvie Dec 16 '13 at 01:05
  • I wrote an answer until I realized that the question doesn't make sense. You've already defined the method once, where should you have to redefine it? – Jeroen Vannevel Dec 16 '13 at 01:07
  • @ChrisDobkowski If x and y were static, then the toString result would be independent of the object, so it would not be much use. – Patricia Shanahan Dec 16 '13 at 01:07
  • @Saposhiente That probably won't be so. If you have some static method `foo()` in class `Foo`, `Foo.foo()` would do the same thing as `someInstanceOfFoo.foo()` – Justin Dec 16 '13 at 01:08
  • @Quincunx It's illegal in this case, as Sotirios says. – Vitruvie Dec 16 '13 at 01:09
  • My psychic powers tell me that you're worried about performance, about all of your instances of your class having "excess copies" of the same function. In actuality, there is only one copy of the code for instance functions; the only difference with static is what the function can access and what can access the function. – Vitruvie Dec 16 '13 at 01:11
  • So bottom line, you can't have a `public void static toString()` because it already exists in the Object class as `public void toString()` and each object is an extension of the Obj class. – Chris Dobkowski Dec 16 '13 at 01:13

2 Answers2

8

This does not apply only to toString()

The Java Language Specification says

It is a compile-time error if a static method hides an instance method.

Since the instance method toString() is implicitly inherited from Object, declaring a method toString() as static in a sub type causes a compile-time error.

From an Object Oriented point of view, see the other answers to this question or related questions.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
5

Because a static method cannot access instance fields. Also, toString() is specified by java.lang.Object, so you must have an instance of Object to invoke toString() on. Finally, if toString() were static, it would have to accept instances of Object (how else could you call toString() on a n instance of a class?).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249