How to call a method from a different class Java?

Calling Methods from Different Classes in Java

Java is a versatile programming language that allows developers to create complex applications with ease. One of the key features of Java is its ability to call methods from other classes. In this article, we will explore how to call methods from different classes in Java.

Understanding Method Calling

In Java, a method is a block of code that performs a specific task. When you call a method, it executes the code within the method and returns a result. To call a method from another class, you need to use the this keyword, which refers to the current object.

Declaring the Target Method

To call a method from another class, you need to declare the target method in the class where you want to call it. Here’s an example:

public class MyClass {
public void myMethod() {
// Code to be executed
}
}

In this example, myMethod is a method declared in the MyClass class.

Declaring the Calling Method

To call the myMethod from another class, you need to declare the calling method in the class where you want to call it. Here’s an example:

public class MyClass {
public void myMethod() {
// Code to be executed
}
}

public class AnotherClass {
public void callMyMethod(MyClass obj) {
// Code to be executed
}
}

In this example, callMyMethod is a method declared in the AnotherClass class that takes an instance of MyClass as a parameter.

Passing an Instance of the Target Class

To call the myMethod from the AnotherClass, you need to pass an instance of the target class as a parameter. Here’s an example:

public class AnotherClass {
public void callMyMethod(MyClass obj) {
obj.myMethod();
}
}

public class MyClass {
public void myMethod() {
System.out.println("Hello, World!");
}
}

public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
AnotherClass anotherClass = new AnotherClass();
anotherClass.callMyMethod(obj);
}
}

In this example, obj is an instance of MyClass and anotherClass is an instance of AnotherClass. The callMyMethod method is called on anotherClass with an instance of MyClass as a parameter.

Passing a Reference to the Target Class

If you want to call a method from another class without creating an instance of the target class, you can pass a reference to the target class. Here’s an example:

public class MyClass {
public void myMethod() {
// Code to be executed
}
}

public class AnotherClass {
public void callMyMethod(MyClass obj) {
obj.myMethod();
}
}

public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
AnotherClass anotherClass = new AnotherClass();
anotherClass.callMyMethod(obj);
}
}

In this example, obj is a reference to an instance of MyClass and anotherClass is a reference to an instance of AnotherClass. The callMyMethod method is called on anotherClass with a reference to obj as a parameter.

Passing an Object Reference

If you want to call a method from another class without creating an instance of the target class, you can pass an object reference. Here’s an example:

public class MyClass {
public void myMethod() {
// Code to be executed
}
}

public class AnotherClass {
public void callMyMethod(MyClass obj) {
obj.myMethod();
}
}

public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
AnotherClass anotherClass = new AnotherClass();
anotherClass.callMyMethod(obj);
}
}

In this example, obj is an object reference to an instance of MyClass and anotherClass is an object reference to an instance of AnotherClass. The callMyMethod method is called on anotherClass with an object reference to obj as a parameter.

Table: Method Calling in Java

Method Calling Declaring the Target Method Declaring the Calling Method Passing an Instance of the Target Class Passing a Reference to the Target Class Passing an Object Reference
Direct Method Calling public class MyClass { public void myMethod() { ... } } public class AnotherClass { public void callMyMethod(MyClass obj) { ... } } MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj); MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj); MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj);
Method Calling with a Reference public class MyClass { public void myMethod() { ... } } public class AnotherClass { public void callMyMethod(MyClass obj) { ... } } MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj); MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj); MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj);
Method Calling with an Object Reference public class MyClass { public void myMethod() { ... } } public class AnotherClass { public void callMyMethod(MyClass obj) { ... } } MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj); MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj); MyClass obj = new MyClass(); AnotherClass anotherClass = new AnotherClass(); anotherClass.callMyMethod(obj);

In conclusion, calling methods from different classes in Java is a straightforward process. By understanding the different ways to declare the target method, passing an instance of the target class, passing a reference to the target class, and passing an object reference, you can write efficient and effective code.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top