How to Create Java Method?

Yosuva ArulanthuJava, Test Automation, Testing, UncategorizedLeave a Comment

Method is collection of code which run only when it’s called.Method also called as Functions.

Method syntax:

	acess_specifiers return_type mathodname()
		//Code to be executed when this method called

Type of methods

Method without return type

Method with return type

public class Test {
		int a=1;
		int b=2;
		String Str1="Hi";
		String Str2="Hello";
		public static void main(String args[])
			Test jc=new Test();
			String s=jc.returnStrings();
			int add=jc. retunrinteger();

		public String returnStrings()
			String s=Str1+" "+Str2;
			return s;
		public int retunrinteger()
			int c=a+b;
			return c;

Method with parameters

Static methods


public class JavaClass {
	// initialization
	int a;
	int b;
	String str1;
	String Str2;
	//main method
	public static void main(String args[])
		JavaClass jc=new JavaClass();
	public void mathodname()

Leave a Reply

Your email address will not be published.