This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine a method using or not using static modifier ?  RSS feed

 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Could anyone tell me how to determine a method using or not using static modifier ?
I have read the Integer class of java api, it contains for example parseInt(String) and intValue() methods. Why parseInt() is a static method but intValue() is a instance method ? I think intValue() can also be static method but don't know why it is not.

Thanks,
Jack
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, let's consider those two methods:
Method parseInt's job is to convert a String into an int (if you want to convert a String into an Integer object, use valueOf). Method intValue's job can be said to be either return the int value of "this" Integer, or to convert an Integer to an int.

Notice in the first case (String |-> int), no Integer object is involved! That's a hint that the method could be static. But, you say, what's it doing in class Integer if no Integer objects are involved? Well, that's because of the affinity between int and Integer -- where else would it go?

Why is intValue non-static? The short answer is that it must be non-static because the following would make no sense!
On the other hand, you way wish to reconstrue it as a static method
That's possible, but it smells bad -- it is a lost opportunity to make a static method non-static. Why prefer making methods non-static when you have a choice? Well, take intValue as an example again. Notice that Integer extends abstract class Number, where intValue is introduced as an abstract method to be implemented by Number's concrete subclasses. One of the advantages to non-static methods is that they can be polymorphic -- something impossible with a static method. Imagine defining your own UnsignedShort class that extends Number and implements its own intValue method. A static intValue3 method in Integer or Number or elsewhere couldn't handle this open-ended Number hierarchy.
[ December 30, 2005: Message edited by: Jeff Albrechtsen ]
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Lau:
Why parseInt() is a static method but intValue() is a instance method ?

It's because parseInt is a class method. It concerns no particular object of the class. It takes a String and returns a new Integer object. It's an object creation method belonging to the class.

intValue on the other hand concerns one particular object. It returns the int value of one specific Integer object.

So methods common to the whole class should be made static, all others not.

Say for example you want a method returning the number of objects that have been created. It should be made static because it returns information belonging to the whole class and not one particular object of the class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!