Passing parameters to methods in another class

import java.util.Scanner;

class jamesprogram {

	public static void main(String args[]){
		Scanner myinput = new Scanner(;
		cat catObject = new cat();
		System.out.println("Enter your name ");
		String name = myinput.nextLine();
public class cat {

		public void showMessage(String name){
			System.out.println("Hello "+name);

So the above program creates a new scanner, then takes the input from the scanner and puts it in a string called ‘name’. This string is then executed in the method showMessage in the cat class.

The output is:

Enter your name
Hello james

Leave a Reply