Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Aaron Law
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aaron Law
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic