aspose file tools*
The moose likes Beginning Java and the fly likes when to use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when to use "new"? (about Inetger.toString()  )" Watch "when to use "new"? (about Inetger.toString()  )" New topic
Author

when to use "new"? (about Inetger.toString() )

Aaron Law
Greenhorn

Joined: Nov 09, 2004
Posts: 3
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?

Thanks

the whole program is as the follow:

[ November 23, 2004: Message edited by: Aaron Law ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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".


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Aaron Law:
Is it because the toString() method is modified as "static"?

That is correct. As Ernest says, new creates a new instance of a class. However, you don't need an instance to use static methods.

HTH

Layne


Java API Documentation
The Java Tutorial
Aaron Law
Greenhorn

Joined: Nov 09, 2004
Posts: 3
Thanks Ernest.

I have had a look in the Java API and find that both of the statements work:

Integer.toString(i+1) ;
new Integer(i+1).toString() ;

As Ernest said, we can pass a parameter (here is the i, a counter of a forloop, and 1 as a value/number of "i+1") into a constructor to initialize an instance of the class Integer.

Is the constructor in the second statement "Integer(i+1)" and the parameter passed is "i+1" ?

Now, I'm wondering the meaning of the 2 statements:

Integer.toString(i+1) ; <-- toString(int i) is a static method so that we can use it without create an instannce of the class/vithout callinng an constructor.

Integer(i+1).toString() ; <-- toString() is a non-static method so that we should create an instance first (here is "Integer() ) and then we can use the instance method "toString()".

Am I right? thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use "new"? (about Inetger.toString() )