How to Create Java Method?

Yosuva ArulanthuUncategorizedLeave 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();
			System.out.println(s);
			System.out.println(add);
		}

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

Method with parameters

Static methods

package com.java.examples;

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.