Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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:

  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

                         

» View all related tutorials
Related Tags: java javascript c array ant script io display ip value number name ole instance this nsis element create elements if

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.