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 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.
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
Here is the code of MainClass.java
Output will be displayed as:
Recommend the tutorial