Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this Holder<> do in Java?

Tags:

java

generics

Can someone please explain this code?

public void getSupplierByZipCode(
    @WebParam(name = "zip", targetNamespace = "http://www.webservicex.net/")
    String zip,
    @WebParam(name = "GetSupplierByZipCodeResult", targetNamespace =  "http://www.webservicex.net/", mode = WebParam.Mode.OUT)
    Holder<Boolean> getSupplierByZipCodeResult,
    @WebParam(name = "SupplierDataLists", targetNamespace = "http://www.webservicex.net/", mode = WebParam.Mode.OUT)
    Holder<SupplierDataList> supplierDataLists);

I've never seen Holder before in Java. What are Holder<Boolean> and Holder<SupplierDataList> in the function? Are they like outputs?? I need the supplier data list from this function.

like image 871
Cim Took Avatar asked Sep 11 '14 01:09

Cim Took


People also ask

What does class <> mean in Java?

What Does Class Mean? A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

What is a holder object?

Holder objects are objects that are created with a single variable. These objects can then be passed into a method that sets that variable. When the method returns, the value of variable can be retrieved.

What are the operators in Java?

Operators in Java. Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Lets take a look at them in detail. Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.

What is the purpose of holding a value?

See Holder - The entire purpose is to "hold a value" while allowing side-effect modifications of itself (and thus change value it "holds").

What are the rules of constructor in Java?

Rules for writing Constructor: Constructor (s) of a class must have same name as the class name in which it resides. A constructor in Java can not be abstract, final, static and Synchronized. Access modifiers can be used in constructor declaration to control its access i.e which other class can call ...

Why do we need an extra layer of reference in Java?

This "extra layer" is required because Java is always Call By Value; the Holder then effectively fakes a pointer-indirection (let's call it "reference-indirection"), as what might be done in C, which leads to Call By (Object) Sharing semantics.


1 Answers

See Holder - The entire purpose is to "hold a value" while allowing side-effect modifications of itself (and thus change value it "holds").

The instance variable (value) representing the contained/"held" value can be reassigned; this is used to facilitate how [multiple] values are "returned" in the WS - through explicit modification of the holders supplied as parameters. (Note the usage of WebParam.Mode.OUT as well.)

This "extra layer" is required because Java is always Call By Value; the Holder then effectively fakes a pointer-indirection (let's call it "reference-indirection"), as what might be done in C, which leads to Call By (Object) Sharing semantics.

Imagine:

// Outside WS function - setup parameters and invoke
String zip = "98682";
Holder<Boolean> result = new Holder<Boolean>();
getSupplierByZipCode(zip, result, ..);

// Then inside the function the Holder is modified and a new value
// is assigned to it's value member.
getSupplierByZipCodeResult.value = true;

// And outside again, the mutations are visibile still
if (result.value) {
    // Yay!
}

Since strings are immutable and the zip is not wrapped in a Holder then the zip code cannot be changed (or "returned" by) the WS call.

See also:

  • Is Java "pass-by-reference" or "pass-by-value"?
  • How to implement int in/out params in java
like image 131
user2864740 Avatar answered Oct 05 '22 19:10

user2864740