Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling instance method without instance

 
venu gopal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have problem understanding the following. This is from Marcus Green Test
Given the following code, which of the options if inserted after the comment //here will allow the code to compile without error?

1. super().output(100);
2 new Wfowl().output(i);
3 class test implements Liz{}
4 System.out.println(sDescription);
5 new Wfowl();
6 getDescription();
Correct answers are 2,3,4,5,6.
Option 6 is an instance method. How could I call without instance? In addition to that It's a void method.
Thanks,
Venu Gopal.
[ August 13, 2003: Message edited by: Thomas Paul ]
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 6: getDescription(); works because Wfowl is a subclass of Wchapel and has inherited it as an instance method.
go() is a void method, getDescription() returns a String, but unless you put 'return getDescription()' the String returned by getDescription() isn't returned by go(). The String becomes an abandoned object, sitting on the side of the datastream, waiting for the garbage collectors to see it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 6 is an instance method. How could I call without instance?
The "// here" comment is inside the go() method, which is an instance method. You can always call an instance method from another instance method, because you must have had an instance to call the other method, so you still have it to call the new method. In this case, go() was called using the instance referred to as "wf"; this instance is used to call getDescription() as well.
In addition to that It's a void method.
Ummm... yes. And this is a problem because... ?
 
tangi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i agree that becos the getDescription is public it can be accessed from it's subclass and has inherited it as an instance method. But as far as that it's a void method i tried to put 'return getDescription()' as Mr.Ray Stojonic has suggested--but was in vain
can anyone please elaborate as how to proceed--as i'm not able to return the string returned by getdescription() by go()
thanx in advance
 
Unni Kainila
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you change the signature the go() method in Wfowl class?
If you want to return the result of getDescription(), you should change the method to:
public String go {
....
retrun getDescription();
}
 
venu gopal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ray and Jim for clarifying my doubts.
Hi tangi I think Ray was trying to explain about abandoned object. If you would like to return the Sring you need to change the return type of go() as
public String go(){
return getDescription();
}


Now wf.go(); is abandoned object
I hope it clarifies your doubts.
Thanks,
venu Gopal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic