score:1
The answer is very simple.
The static
read method allows you to call the method on the actual class and return an instance. So, instead of doing:
MyWritable writer = new MyWritable();
writer.readFields(input);
You can just do:
MyWritable writer = MyWritable.read(input);
And achieve the same result. It's for convenience.
Then, it returns a MyWritable
because otherwise you'd never get the object! The first method can return void
because you have already created an object instance, but in the latter, it has to return the instance it creates for you.
score:0
Effectively, it looks as though they are trying to create a simple entry point for reading these items instead of 'new' ing up the unknown writable. So: MyWritable.read(dataInput);
which creates an instance in memory of MyWritable (lets call it myWritable1). So then they invoke 'myWritable1.readFields' and allow it to 'read' and 'populate' itself.
MyWritable.read(dataInput) will RETURN myWritable1 (created above).
The static read method is completely unnecessary and probably not even a good idea. It makes it easy to use, but it's not really saving much work. new MyWritable().readFields(dataInput) would work just as well.
score:0
- It returns a
MyWritable
so you can do stuff with it after you read, like look at the data. - It's static because it's a factory/convenience method.
- The others don't because you'd already have an instance if you were calling instance methods.
score:0
In this case the read(DataInput)
method is a factory method that creates a MyWritable
based on some data. It has a return type because it returns the object it creates. It is static because a non-static factory method does not make much sense (how would you call a method on an object which has not yet been created?)
score:1
This design implements a very simple factory method pattern: the users would call read
, rather than calling new MyWritable()
followed by readFields()
.
To complete the factory method pattern implementation, make the constructor private
, and return the constructed object by interface:
private MyWritable() {} // Disallow external instantiations
// Use the Writable interface rather than MyWritable as the return type
public static Writable read(DataInput in) throws IOException {
MyWritable w = new MyWritable();
w.readFields(in);
return w;
}
Source: stackoverflow.com
Related Query
- auto-generate code for delegation/encapsulation class (in Intellij)
- Java class design for redundant variable property
- Can I customize lombok to writing code for each variable in class similar to getters and setters?
- Can I customize lombok to writing code for each variable in class similar to getters and setters?
- how find the number of conditions in a control structure for a given code segment in java
- how find the number of conditions in a control structure for a given code segment in java
- how can i code public class for SimpleCursorAdapter?
- How to modularize the code for JsonObject and JsonArray to read Json files at class level. Which I am using later to write Testcases
- question about accessing arraylist for another class
- Execute same piece of code in catch clause for all methods in a class
- A class is looking for methods of another class in the maven repository instead of the source code
- Design pattern for searching object in class of objects without comparing all objects in java
- java skeleton code from design class diagram. Remove error
- Better design approach for having a variable in a class not present in Database
- OO Class Design for ATM card
- Sample Code for usage of BasicMDCAdapter class for java.util.logging framework
- Class level design for validation engine with 2 independent entities
- Class level design for validation engine with 2 independent entities
- Need code that counts variables from a method in a class generated randomly for each time the program is run
- Unable to set the custom response code in class mediator and xml wso2 apim 3.2.0
- How to create a POJO class for dynamic data type of field in Java?
- java test runner for vs code stuck at resolving launch configuration
- Malicious code vulnerability - May expose internal representation by returning reference to mutable object for int[]
- How to properly code this one to many relationship according to design
- how to create separate methods for function without effecting the code
- Issues with Math.abs in Chess code for Rook
- One method in static class for 2 (two) condition. without new var
- Using generics for single implementation of inheritance from classes with a common parent class
- Cannot find SyncConfiguration (or equivalent class for Realm)
- How to use selenium Script code for each script?
More Query from same tag
- VLCJ-Pro Java returned: -57005
- Unable to launch chrome via chrome webdriver on jenkins where as it works fine on my local machine
- How to cast an array of abstract classes by an array of interfaces
- Orientation change of multiple fragments in activity?
- How to read an array inside an array inside an array from a volley response?
- android.os.TransactionTooLargeException when executing queryIntentActivities
- How to cache image downloaded from URL in Java?
- How to condensely compare four different strings to see if there are any matches?
- Java Income Tax Calculator Need to add error message for any number that is 0 or less
- WSO2IS:Token generated with grant_type value password of one user, coming active for another users
- Jboss 4.2.3 Application with huge number of anonymous Threads
- Maven Read properties plugin doesn't substitute values for dependencies
- Replaced method with long parameter list with builder pattern. How to test it with Mockito?
- Chain multiple network calls and database inserts with Room and RxJava in Android
- com.jcraft.jsch.JSchException: java.io.IOException: Pipe closed
- Best way to Query for single Entity based on ONE property value?
- How to generate html image using JUnit Testrunner
- How can I create a certain amount of edit texts staying next to each other programmatically in Android?
- How do I get the Intersection method to work?
- NoSuchMethodException for methods using custom data type
- unable to Initialize AccountKit android studio. Initialization Error 501
- Spring ResourceHandlerRegistry multipleLocations and performance
- Can not get places data from google map in android
- SQL - Convert a String into a Text
- Maven - URI is non hierarchial
- React Native error: invalid Java _Home with genymotion?
- Java - The method newDefaultUnpacker(byte[]) is undefined for the type MessagePack
- setCellValue(String value) throws java: cannot access java.time.LocalDate class file for java.time.LocalDate not found
- Implemented Iterable hands wrong size back
- Incorrect mapping of a texture on a rectanlge in Open GL ES