• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling method from another class  RSS feed

 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to call a method(with arguments) from another class without inherinting that class
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shalini gnana:
How to call a method(with arguments) from another class without inherinting that class


If both the class reside in current working directory then you can directly create an object of that class and call its methods using that object.
Otherwise in shell prompt you can use -classpath option of java to get that class.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a reference to an object of the other class. Your class could get that either as a parameter in its constructor, construct a new instance of it, or get a reference through a setter method like below. Of course, setTheOtherObject must be called before myMethod can run.


[ January 03, 2008: Message edited by: Ulf Dittmer ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!