PiotrChernin
PiotrChernin

Reputation: 501

How do I interpret a getter method used as a parameter?

I have a bit of Java code that I can't follow. Two classes, ClassA and ClassB, structured like this:

ClassA {

    void setName() {
        this.name = name;
    }

    String getName() {
        return name;
    }

    void writeFunction(Object value) {
        String v = value.toString();
    }
}

and

ClassB extends ClassA {
    ...
    writeFunction( getName() );
    ...
}

I've never seen a getter method used without an object reference, so I'm not certain what's happening here. getName() can only refer to ClassA.getName(). My understanding is that methods can't be passed as parameters in Java, which means that the argument to writeFunction() must be the result of the method, presumably as this.getName?

Can anyone make an educated guess what's happening here? JavaBeans and JSP tag libraries are involved, if that means anything.

Edit: Added details to code

Upvotes: 0

Views: 64

Answers (1)

Mureinik
Mureinik

Reputation: 311843

Calling an instance method from within the class that defines it calls it on the current instance (i.e. this). Here, you call getName() on the current ClassB object (the method is inherited from the super-class, ClassA), and then pass the returned value to writeObject.

Upvotes: 2

Related Questions