I’m using JSoup and I’m doing some node traversal.
String myString;
NodeTraversor articleNodeTraversor = new NodeTraversor(new NodeVisitor() {
@Override
public void tail(Node node, int depth) {
//Do some processing.
myString += "foo";
}
@Override
public void head(Node node, int depth) {
//Do some processing.
}
});
Firstly, what is this kind of programming called? Visitor pattern? Secondly, the above code only works if myString is global. How can I make my string local to only the method it is being created in and still make the above work?
Thanks!
You are using anonymous classes, so they can access only final local variables outside of its context ( assuming its declared within a method )and any variable of the class ( static or non-static). Also you can declare variables within the inner class, BUT static non-final variable declarations are not allowed.