PC Tips site


Java: Methods:

Java: Methods


Method Header:
A method header like:
public static void main(String args[])
consists of access specifier (eg. public), special keyword (eg. static), return type (eg. void), method name (eg. main), parameter types (eg. array of String), and parameter names (eg. args)

Access specifiers: tell Java the 'visibility' of your method to other classes using the current class
Types of access specifiers:

Static keyword:

it can be used for methods or for variables.

the static keyword is used to ensure all objects created from a class share the same method or variable.
methods or variables which are to be called or used by main method (itself a static method) must be static, so they must also be declared with static keyword.

Return type: the type of data (can be a primitive data type such as int, double, etc or an object such as String, Integer, etc) that is returned by the method (return type of void means the method doesn't return anything)

Parameters and Arguments: parameters are the names which appear in the method header, and arguments are the actual values passed to the method when it is called.

 

Notes:

client program: a program that creates an object from an instantiable class

Contents of methods/MtdSimple.java file


package methods;

public class MtdSimple
{

	public static void main(String[] args)
	{
		boolean msgDisplayed;
		msgDisplayed=displayMsg();
		System.out.println("value of msgDisplayed is "+msgDisplayed);
	}
	
	public static boolean displayMsg()
	{
		System.out.println("displayMsg mtd says Hi, and returns true");
		return true;
	}
}

Example output is:

displayMsg mtd says Hi, and returns true
value of msgDisplayed is true

In the above code, displayMsg executes (outputs "displayMsg mtd says Hi, and returns true"), and returns a boolean value of true, which is assigned to boolean var called msgDisplayed, and main method then uses the value of msgDisplayed to output what the value of msgDisplayed is.

top of page


Parameters and Arguments

Parameters:

Contents of methods/MtdWithParams.java file


package methods;

public class MtdWithParams
{

	public static void main(String[] args)
	{
		displayMsg("Trigun", "great story and action seqs");
		displayMsg("Outlaw Star", "kickass action");
		displayMsg("Gundam Seed", "great drama and action");
	}
	
	public static void displayMsg(String show, String desc)
	{
		System.out.println("I feel "+show+" has "+desc);
	}
}

Example output is:

I feel Trigun has great story and action seqs
I feel Outlaw Star has kickass action
I feel Gundam Seed has great drama and action

In the above code, displayMsg executes using the values passed to it to output the corresponding message.

top of page


Arguments are passed by value:

Contents of methods/MtdWithArgAdv.java file


package methods;

public class MtdWithArgAdv
{

	public static void main(String[] args)
	{
		String show = "Trigun";

		displayMsg(show);

		System.out.println("in main mtd, show = " + show);
	}

	public static void displayMsg(String show)
	{
		show = "Gundam Seed";

		System.out.println("in displayMsg mtd, show = " + show);
	}

}

Example output is:

in displayMsg mtd, show = Gundam Seed
in main mtd, show = Trigun

In the above code, in main mtd, var called show is set to "Trigun", and this value (and only the value) is passed to displayMsg mtd.
In displayMsg mtd, var called show is set to "Gundam Seed", and the value of show is printed as 'Gundam Seed'.
However, in main mtd var called show still remains as "Trigun", and the value of show is printed as 'Trigun'.

top of page


Method Overloading

Contents of methods/MtdOverloading.java file


package methods;

public class MtdOverloading
{

	public static void main(String[] args)
	{
		displayMsg("Cowboy Bebop");
		displayMsg("Naruto", "a ninja fight series");
	}
	
	public static void displayMsg(String show)
	{
		System.out.println(show+" is a great story");
	}
	
	public static void displayMsg(String show, String desc)
	{
		System.out.println(show+" is "+desc);
	}
	
}

Example output is:

Cowboy Bebop is a great story
Naruto is a ninja fight series

In the above code, there're 2 versions of displayMsg method, differentiated by the method signature, which consists of the uniqueness of the number and type of parameters (method signature is not dependent on return type, or parameter names)

top of page