0

I have the following code excerpt in the main method:

CsparqlQueryResultProxy c1 = null;
c1 = engine.registerQuery(query, false);
c1.addObserver(new RDFResultsProcesser()); 

In order to get the Observer argument values I created a new class as an extension to ResultsFormatter like follows:

public class RDFResultFormatter extends ResultFormatter {
    @Override
    public void update(Observable o, Object arg) {
        RDFTable res = (RDFTable) arg;
        for (final RDFTuple t : res) {
            String rdfName = t.get(0);
           String rdfVal = t.get(1);
        }
    }
}

But, I cannot return back the values of rdfName and rdfVal in main and I know its because of the fact that update is a void function. Does anyone has any idea how can I return these values in main?

Edi
  • 109
  • 11
  • Returning a value (in the usual sense) implies that you have a chain of calls from one point in your program to a method where that value is produced. An Observer is called due to some change triggered by code that isn't really interested in the changed values as it has initiated that change in the first place. So there is no way something observed by an Observer can be "returned" to some place in your main program. You'll have to think of a way of relaying that name/value data to some entity in your program that may then do whatever should be done, and the main prog will have to look for it. – laune Dec 08 '15 at 16:32
  • Is there a way to update a main variable from within Observer? – Edi Dec 08 '15 at 16:41
  • Could you use the visitor pattern? Check [this link](http://stackoverflow.com/questions/2604169/could-someone-in-simple-terms-explain-to-me-the-visitor-patterns-purpose-with-e) for a great example. – John Harris Dec 08 '15 at 16:59
  • 1
    `main` is a method name. Any variable declared within it has method scope and method lifetime, and their references can be passed via calls to other methods. An Observer can be created with references passed to it, and it can use these references to modify objects. – laune Dec 08 '15 at 17:00
  • But there is no distinction in java between pass by reference and pass by value @laune – Edi Dec 08 '15 at 17:43
  • Everything is passed by copying a value, including the "values" that are references to objects. – laune Dec 08 '15 at 17:51

0 Answers0