aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling instance method without instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling instance method without instance" Watch "Calling instance method without instance" New topic
Author

Calling instance method without instance

venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
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

Joined: Aug 08, 2003
Posts: 326
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

Joined: Jan 30, 2000
Posts: 18671
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... ?


"I'm not back." - Bill Harding, Twister
tangi
Greenhorn

Joined: Aug 13, 2003
Posts: 4
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

Joined: Jul 26, 2003
Posts: 45
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

Joined: Nov 21, 2000
Posts: 40
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling instance method without instance