What does the naming convention 'of' mean in Java?

Solution 1:

It is an English word, yes. It is usually chosen so that the expression will read like an English phrase. For example, Stream.of(3, 4) is supposed to look like "stream of three and four," which is sort of like a shortened version of "a stream that is made of the numbers three and four."

Solution 2:

As traditionally if you look into wrapper classes, they all contain valueOf(XXX) method to build an instance of the wrapper class of the given value type.

Integer.valueOf(int)
Double.valueOf(double)
Float.valueOf(float)

Java is following this naming convention from day 1. Similarly most of Java-8 introduced classes contains this of(...) method heavily.

Stream.of(...)
LocalDate.of(year, month, dayOfMonth)
Instant.ofEpochSecond(epochSecond)
and many more.

There are not only valueOf or of methods available, it has few more methods that serves a specific purpose or type of task inside different classes and they have assigned with a best suitable name representing that task.

  • parseXXX(): For parsing given string input. Examples: Integer.parseInt(str), Double.parseDouble(), Date.parse(datestr) etc
  • get(field): Retrieving a field information from the object. Example: Calendar.get(field), LocalDate.get(TemporalField) etc.
  • format(): Converting to different representaion. Example: String.format(), SimpleDateFormat.format(), DateTimeFormatter.format() etc

These naming conventions are heavily utilized in Java8 DateTime API. Check out this Method Naming Conventions

Solution 3:

It is a common naming convention used for static factory methods.

Joshua Bloch mentions the following about the pattern and associated naming conventions in Effective Java (2nd Edition), Item 1: Consider static factory methods instead of constructors (emphasis added):

... (a) disadvantage of static factory methods is that they are not readily distinguishable from other static methods. They do not stand out in API documentation in the way that constructors do, so it can be difficult to figure out how to instantiate a class that provides static factory methods instead of constructors. The Javadoc tool may someday draw attention to static factory methods. In the meantime, you can reduce this disadvantage by drawing attention to static factories in class or interface comments, and by adhering to common naming conventions. Here are some common names for static factory methods:

  • valueOf—Returns an instance that has, loosely speaking, the same value as its parameters. Such static factories are effectively type-conversion methods.

  • of—A concise alternative to valueOf, popularized by EnumSet (Item 32).

...

So, as others have pointed out, "of" means the English word "of", and is not an abbreviation. But one of the reasons for using this convention is to make it easier to find out if a class provides a factory method because static factories don't show in a separate section in the JavaDocs (compared to constructors).

An added benefit, in my opinion, with using concisely and desciptively named static factories, is that it makes the code read like prose, which isn't the case if it's littered with new constructor calls.