How does one method work within another? E.g: service.metodo (arguments) .execute ();

4

I am not understanding this method within another method does anyone explain me this?

Another example of my question:

ImageKeywords keywords =  service.getImageKeywords(image, forceShowAll, knowledgeGraph).execute();

It has a method that calls another method inside it.

    
asked by anonymous 27.06.2016 / 16:26

1 answer

5

There is no method within the other, there is a sequence of methods.

With the object service it calls the method getImageKeywords() by passing some arguments. It will produce a result (usually methods return something), this result is an object that can invoke other methods, in which case it is invoking execute() in the object returned by getImageKeywords() . The result of execute() is that it will be saved in the keywords variable.

This code could be written like this (or almost, I used var , which you do not yet have in Java , because I do not know the type of method):

var temp = service.getImageKeywords(image, forceShowAll, knowledgeGraph);
ImageKeywords keywords = temp.execute();

Most visible example:

public static void main (String[] args) {
    String x = teste().toString(); //teste retorna um inteiro 42 que é usado pelo toString
    System.out.println(x); //x já é uma string "42"
}
public static Integer teste() {
    return 42;
}

See working on ideone and on CodingGround .

One thing people often do not realize is that variables serve to hold intermediate values. There are cases where they are necessary, or at least more advantageous, but they do not always need to be used. An intermediate value of an expression can be used directly where it is needed without the use of a variable.

An expression is usually composed of subexpressions that produce intermediate values. Pure math.

    
27.06.2016 / 16:35