13.3 Constructor References
To create an object, you use the new keyword. This keyword entails the call of a constructor, which can optionally be passed arguments. However, the Java API also declares types from which no direct objects can be created via new . Instead, factories are tasked with creating objects. These factories can be static or non-static methods.
Typical examples of constructors include the following:
-
new StringBuilder( “1” ) → StringBuilder
-
new File( “dir” ) → File
-
new BigInteger( val ) → BigInteger
Examples of factories include the following:
-
LocalDate.of( 2022, 02, 02 ) → LocalDate
-
Paths.get( “dir” ) → Path
-
BigInteger.valueOf( val ) → BigInteger
Both constructors and factories can be thought ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access