In Effective Java, it mentions that "Unlike constructors, static factory methods are not required to create a new object each time they are called."
class Car{ String color; Boolean spoiler; public Car(String s){ color=s; spoiler = false; } public static Car redCar(){ return new Car("red"); } }
In the main class:
Car c2 = Car.redCar(); Car c3 = Car.redCar();
c2 and c3 are different objects. I did not get the context "it is not required to create a new object with every call."
Because you are doing this:
public static Car redCar(){ return new Car("red"); } // ^ here
If you want to return the same value, you can do something like:
private static final Car RED_CAR = new Car("red"); public static Car redCar(){ return RED_CAR; }
The fact is that the call new Car()will always return a new instance. A call Car.newInstance()means that the class Carcan decide what to do.
new Car()
Car.newInstance()
Car
For instance:
private static final Map<String, Car> CARS = new HashMap<>(); public static Car newInstance(final String colour){ return CARS.computeIfAbsent(colour, Car::new); }
Car Map.computeIfAbsent, , Car Map. () .
Map.computeIfAbsent
Map
:
final Car one = Car.newInstance("red"); final Car two = Car.newInstance("red"); System.out.println(one == two) // true
, - , , factory . ( ):
class Car { String color; public Car(String color) { this.color = color; } public static Car car(String color) { Car car = CARS.get(color); if (car != null) return car; car = new Car(color); CARS.put(color, car); return car; } private static final Map<String, Car> CARS = new HashMap<>(); }
Integer factory valueOf. , factory ( ).
Integer
valueOf
" factory , ". , factory ( ), , ( ). p >
, , redCar() -, :
redCar()
class Car{ /* snipped */ private static final RED = new Car("red"); public static Car redCar(){ return RED; } }
,
return new Car("red");
factory factory.
, , . "" , ; .
unlike constructors static factory methods are not required to create a new object each time they're invoked", , new; , , "" .
unlike constructors static factory methods are not required to create a new object each time they're invoked"
: ""; ; Java . , .
, .
Factory - . , , factory.
, singleton . , File, file1.txt, singleton ( ). , file2.text, singleton. File, file1.text file2.text, .
, (, ). , Singleton ( ) , .
, - factory , , ( - ), , , , .
, factory. ( ). factory, ClassName.factory .
The idea of ββBloch describes that a static factory can use the pool or cache of instances that it passes on request or makes decisions about its internal logic to create a new instance (which can cache too). Usually this only works for immutable objects , because otherwise you will have some kind of opaque cross-objects.
The implementation you specified is not a static factory. You have created a class as shown below:
class Car{ String color; Boolean spoiler; public static final Car car = new Car("name"); public Car getInstance(){ return car; } private Car(String s){ color=s; spoiler = false; } public static Car redCar(){ return new Car("red"); } } and then in main you have to call Car.getInstance();
Source: https://habr.com/ru/post/1584014/More articles:Cannot override afterRequest method on Ext.data.proxy.Ajax instance when reconfiguring Ext JS 4 code base - javascriptOracle char datatypes not working with Entity Framework - c #Oracle selects CHAR data type - sqlODP.NET/EF6 - CHAR data type in WHERE clause - c #how to write c # code in sitecore contenteditor rules - c #How to make AngularJS Material Grid List horizontal rather than vertical? - javascriptSearch in sleep mode - SearchFactory not initialized - javaConvert image formats and resize images in Java - javaseparate all working copies and reset all version history in Xcode 6.3 - gitManipulate JSON in Laravel 5 Middleware - jsonAll Articles