Why is String's format(Object... args) defined as a static method?

12,103

Solution 1

Perhaps "%02d".format(5) seems to imply that the object on which the format method is being called is a format string.

In this case, the format string happens to also be a String, so furthering that point, one could argue that all Strings are format strings.

Probably this can be avoided by saying that a static method in the String class can be used to format a string, rather than making some implicit statement about all Strings in general.

Solution 2

While I am not a designer of Java, I can tell you one clear reason for making it static.

Java 5 came out with many features, but two of note were:

  • The ability to perform "import static" commands that allowed static methods to be used from within a class easily and without listing their class name.
  • A static utility method that performed printfs easily.

While it'd be nice to be able to say "bla: %d".format("foo"), by making the method static you can use format in a way that's very familiar and clean-looking to C programmers used to printf().

import static java.lang.String.format;

public class Demo {

   public void Test() {

      //Do some stuff

      format("Hey, this is easy to read!");

   }
 }

And that's why! By using static imports, printfs look almost exactly like they do in C. Awesome!

Solution 3

The main reason is probably that the designers of Java did not want to add too much stuff to the interface of String. Making it a member function would have meant putting it on string. Remember that a non-static method would have to be on the String object.

A second reason is that the static format maintains its resemblance to C's printf, which looks like printf(FORMAT, ARG1, ARG2...)

Another reason is that format is overloaded: there is a version that takes locale as the first parameter (before the string), so doing this on the string object would be tricky.

Solution 4

"%02d".format(5) would look like "%02d" is formatted using the format 5 rather than the opposite. Also most strings are not suitable as format ("hello world".format(5)?), so the method should throw exceptions for most string objects.

Solution 5

Answer rests in responsibility of Format method.

It is more logical and intuitive, at least to me, to say that "format string is input to Format method" than to say that "Format operates on format string". This is so, as we "usually" pass format string, known at design time, to Format. In contrast, for Trim, we "usually" pass variable string whose value is not known at design time.

So, making format method static, make code reading more intuitive. Look at following (C#).

answer = String.Format("This is format string : {0}", someValue); 
//More readable to me

answer = "This is format string : {0}".Format(someValue);

EDIT: Even though I have used C# example code, it applies well to Java. I got -ve vote for using C# syntax!

Share:
12,103
Russell Ladd
Author by

Russell Ladd

Android Developer since 2009.

Updated on June 08, 2022

Comments

  • Russell Ladd
    Russell Ladd almost 2 years

    I wonder why Java 5 and above provide a printf-style formatter using a static method in class String like this:

    public static String format(String format, Object... args)
    

    instead of

    public String format(Object... args)
    

    so that we can write "%02d".format(5) to get 05 instead of String.format("%02d", 5).

    I imagined if I could modify the String class, I could add this:

    public String format(Object... args) {
        return format(this, args)
    }
    

    to get the same result.

    I found that in C#, a static method is also used instead of an instance method.

    I wonder why they decided to do this, but I didn't come to an explanation. The instance methods trim and substring returns a new instance of string, so they should have done the same thing with format.

    Moreover, the DateFormat class also uses this:

    public final String format(Date date)
    

    for formatting dates. So if we consider the instance of DateFormat as the formatter, an instance of String could also be used as a formatter.

    Any ideas?