Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javascript-array Java call method from another class

Related Tutorials


 
 

Share on Google+Share on Google+

Java call method from another class

Advertisement
In this section, you will study how to access methods of another class.

Java call method from another class

     

In this section, you will study how to access  methods of another class. For this we have created two java files:

  1. CallingMethod.java
  2. MainClass.java

In the example, five methods, namely : add, subtract, multiply, division and modulus have been created inside the class CallingMethod under CallingMethod.java file. Now for getting access to CallingMethod class methods inside the class MainClass, an object of the CallingMethod class of CallingClass.java file has to created inside the class of MainClass.java file as shown in the example.

 Syntax of creating object : ClassName objectName = new ClassName();

After creating object of CallingMethod.java file class CallingMethod inside the class of MainClass.java file. Its very easy to access the methods of the class CallingMethod. With just the object name along with a dot operator, any method at  a time can accessed, as illustrated in the example below.

Syntax for calling methods : objectName . methodName();

Note : Base directory of both the java files should match with each other.

Here is the code of CallingMethod.java

public class CallingMethod{
int c;
public int add(int a, int b){
return a+b;
}
public int subtract(int a, int b){
return a-b;
}
public int multiply(int a, int b){
return a*b;
}
public int division(int a, int b){
return a/b;
}
public int modulus(int a, int b){
return a%b;
}
}

Here is the code of MainClass.java

public class MainClass {
public static void main(String[] args){
CallingMethod method = new CallingMethod();
System.out.println("Addition: " + method.add(30,15));
System.out.println("Subtraction: " + method.subtract(30,15));
System.out.println("Multiplication: " + method.multiply(30,15));
System.out.println("Division: " + method.division(30,15));
System.out.println("Modulus: " + method.modulus(30,15));
}
}

Output will be displayed as:

call methods in java

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 19, 2008

Related Tutorials

Discuss: Java call method from another class   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
bipin_tandel
December 24, 2011
call sql connection in another class

hi, i want to create one connection method and use in all classes how can i do it........?
Rais
January 7, 2012
Thanks !!

Thanks, this helped a lot. It was very easy to understand.
Ximalog
March 15, 2012
His good but had coding

Why don't we make room for inputting any values for int and int b using eg swing or reading in from cmd.thanks
Qafqaz
April 3, 2012
Time problems

I have a question. For example i have two same methods, but one of them in other class , and other one is my class. I call both of them but first one take more times to return any value. My question what is difference between the class? And why first one take more time than second one?
Magnus
April 21, 2012
Thanks

Thank you so much! I've been trying to get sound files to play in my game and this helped me call the sound method. :) Thanks!
Rozhan
May 13, 2012
call method

in this ex:CallingMethod{ I con't find error 25,27-32{S.o.p....}, pleas help to understand Method..
pakyow
September 11, 2012
ULOL

eh tang ina mo gusto mo 1v1 tayo sa programming?
Cedric
November 21, 2013
Thank you !

Thank you ! I'm a beginner and this example helps me to undestrand how to call method in other class ^^
martin
June 4, 2014
java

its easy to understand thanks
DMCA.com