Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-15-2008, 01:01 AM
antgaudi's Avatar
Member
 
Join Date: Sep 2008
Location: Houston-TX
Posts: 22
Rep Power: 0
antgaudi is on a distinguished road
Default [SOLVED] Call method from another class name
With this code:
public class First
{
public void myFirst()
{
}
}


The following is the CORRECT
statement used if you want to call
method myFirst() from another class
named “Second”?
a. First(myFirst);
b. Second (myFirst);
c. First.myFirst ( );
d. Second.myFirst ( );
e. The correct statement is not listed.

The answer is "e"...
What would be the actual correct statement?

Thanks.
__________________
"The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-15-2008, 01:05 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
To call a non-static method in another class, you need a reference to that class.
First aRef = new First(); // create new instance and set reference
aRef.myFirst(); // call the method

If you already have a reference to that class, you don't need to create it. A reference can be passed in a method call:
public void calledMethod(First aRef) {
aRef.myFirst();
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-15-2008, 01:07 AM
antgaudi's Avatar
Member
 
Join Date: Sep 2008
Location: Houston-TX
Posts: 22
Rep Power: 0
antgaudi is on a distinguished road
Default
what if I needed to call one that is static?
__________________
"The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-15-2008, 01:33 AM
Senior Member
 
Join Date: Aug 2008
Posts: 372
Rep Power: 2
Supamagier is on a distinguished road
Default
than it would be c
__________________
I die a little on the inside...
Every time I get shot.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to dynamicaly get all parameters in a method call. DavidJohns Advanced Java 2 05-21-2008 08:15 AM
Call Java Method hussainzim JavaServer Pages (JSP) and JSTL 1 05-15-2008 08:22 AM
cannot call private method from static method jon80 New To Java 3 05-07-2008 09:37 AM
Call a Method Automatically rhm54 New To Java 4 02-07-2008 09:51 AM
Unablt to call a sessionbean's business method in EJB 3.0 amitid4forum Enterprise JavaBeans 0 11-24-2007 01:00 PM


All times are GMT +2. The time now is 12:01 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org