Call a Method in Another Class in Java

Hello Guys How Are You All? Somethimes We need to use Our Class1 Method in Our Class2 So Here is We are going to learn about Call a Method in Another Class in Java With Easiest and Simplest ever Example So That Lets Start this tutorial Without Wasting Your time.

Here We are going to Use Three Type of Method:

  1. First One is Static Method
  2. Second One is protected Method
  3. And Third One is public Method

Lets Understand all Three method with Example.

Call a Method in Another Class in Java

Call a Method in Another Class in Java

To Call a Method in Another Class in Java In this Method we are going to use static method. Lets suppose We have GetName() class and GetName() has getUserName() Method. We Need To use getUserName method in Our Main Method. Here is My Example Code to achieve this.

How to Call a Method in Another Class in Java?

To Call a Method in Another Class in Java Here We are Going to use Private method Here. We Just Use our Method as Private method. You cant use it directly You have to Extend this class Inorder to use GetName Class’s getUserName method. Here is My Example.

Method 1: Using Static

In this Method we are going to use static method. Lets suppose We have GetName() class and GetName() has getUserName() Method. We Need To use getUserName method in Our Main Method. Here is My Example Code to achieve this.

class GetName {
	static String userName;
	
	static String getUserName() {
		return userName;
	}
}

public class App {
	public static void main(String[] args) {
		GetName.userName = "ittutorialpoint_7";
		String myUserName = GetName.getUserName();
		System.out.println("My Username is : " + myUserName);
	}
}

Output Of my above code.

My Username is : ittutorialpoint_7

Method 2: Using Protected

We are Going to use Private method Here. We Just Use our Method as Private method. You cant use it directly You have to Extend this class Inorder to use GetName Class’s getUserName method. Here is My Example.

class GetName {
	protected String userName;

	protected String getUserName() {
		return userName;
	}
}

public class App extends GetName {
	public static void main(String[] args) {
		App usrnm = new App();
		usrnm.userName = "ittutorialpoint_7";
		String myUserName = usrnm.getUserName();
		System.out.println("My Username is : " + myUserName);
	}
}

Output Of my above code.

My Username is : ittutorialpoint_7

Method 3: Using Public

When our method is declared as Public method then it is available for outside access and can be called into another class. You can See in example we have public method named getUserName in the GetName class and we have to use getUserName in Our App class. Here is Example.

class GetName {
	public String userName;

	public String getUserName() {
		return userName;
	}
}

public class App {
	public static void main(String[] args) {
		GetName usrnm = new GetName();
		usrnm.userName = "ittutorialpoint_7";
		String myUserName = usrnm.getUserName();
		System.out.println("My Username is : " + myUserName);
	}
}

Here is Output Of my above code.

My Username is : ittutorialpoint_7

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth Patel

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment