Java call method from another class

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:


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

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

After creating object of file class CallingMethod inside the class of 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

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

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

Share on Google+Share on Google+

Java call method from another class

Posted on: November 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java call method from another class   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
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........?
January 7, 2012
Thanks !!

Thanks, this helped a lot. It was very easy to understand.
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
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?
April 21, 2012

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!
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..
September 11, 2012

eh tang ina mo gusto mo 1v1 tayo sa programming?
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 ^^
June 4, 2014

its easy to understand thanks