how to call a method defined in another file?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 8
Reputation: dajiebuda is an unknown quantity at this point 
Solved Threads: 0
dajiebuda dajiebuda is offline Offline
Newbie Poster

how to call a method defined in another file?

 
0
  #1
Jun 16th, 2007
codes in two files. Is there a way to use a method cross files?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: how to call a method defined in another file?

 
0
  #2
Jun 16th, 2007
The class calling the method needs a reference to the other class. For example, ClassA instantiates ClassB (Below) and ClassB needs to call a method provided by ClassA. The 'this' keyword can be passed into ClassB's constructor to provide it with a reference to ClassA.

E.g.

  1.  
  2. public class ClassA{
  3. ClassB subordinate;
  4.  
  5. public static void main(String[] args){
  6. subordinate = new ClassB(this);
  7. }
  8.  
  9. public void mymethod(){
  10. //do something
  11. }
  12. }
  13. public class ClassB{
  14. ClassA master;
  15.  
  16. public ClassB(ClassA master){
  17. this.master = master;
  18. master.mymethod();
  19. }
  20. }
Last edited by Cerberus; Jun 16th, 2007 at 4:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 8
Reputation: dajiebuda is an unknown quantity at this point 
Solved Threads: 0
dajiebuda dajiebuda is offline Offline
Newbie Poster

Re: how to call a method defined in another file?

 
0
  #3
Jun 16th, 2007
This is very helpful! Thanks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: how to call a method defined in another file?

 
1
  #4
Jun 17th, 2007
but don't go that way. Learn about proper Object Oriented design rather than trying to use Java as if it were a procedural language.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: how to call a method defined in another file?

 
0
  #5
Jun 17th, 2007
Originally Posted by dajiebuda View Post
codes in two files. Is there a way to use a method cross files?
what do you mean with use? you can call it by creating the instance of that method
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC