I'm coding on GUI and wondering when to "new" sth:
In the last line, I assume that I should "new" an Integer object and then use its toString() method to convert the "i+1" of primitive type into a String object, but the compiler doesn't allow me to do so. Is it because the toString() method is modified as "static"? the API
I just wonder why don't we need to "new" it. If the "toString" is not static, should we "new" it in order to return a new object of String type?
the whole program is as the follow:
[ November 23, 2004: Message edited by: Aaron Law ]
author and iconoclast
The "new" operator lets you create an instance of a class by name, like "new Button()". It also allows you to pass arguments to a constructor of a class -- a special sort of method that has the same name as the class its in, but no return type. The new object is then initialized by calling the constructor.
On the other hand, when you're just calling a method -- for instance, the "toString()" method of the "Integer" class -- the "new" operator isn't involved. The toString() method creates a String and returns it -- and it probably does so by using the new operator itself!
Perhaps the simplest way to answer your question is just to say that if you're trying to make an instance of a class by name, then you use "new". If you're just calling a method, you don't use "new".