You are on page 1of 26

Methods

Method
A method is a group of programming language statements with a given name.

Method syntax
Returntype methodname (parameters) { statement 1; statement 2; .. }

Method has two major parts: 1. Method Declaration 2. Method Body


Declaration

Body

void sum ( int a, int b ) { statement 1 statement 2 .. }

Method that does not return a value


If a method does not return a value, its return type must be declared void. For example:
void abc( ) { System.out.println(Hello World); }

Method that returns a value


Methods that have a return type other than void return a value to the calling routine using the following form of the return statement. return value;
int abc( ) { return 5+5; }

Class Methods
(static methods)

Creating Static Methods The class (static) methods is not referenced through a particular instance of a class but through the class itself. You dont have to create an object of the class to invoke a static method.

Static Method Syntax


static Returntype methodname (parameters) { statement 1; statement 2; .. }

example
)static void display(int a, int b { System.out.println(a+b); }

Class Methods
vs.

Instance Methods

Static vs. Instance Methods


Static Method Instance Method
methods are without static

Static methods are declared instance by using static keyword. declared keyword.

All objects share the single All objects have their own copy of static method. copy of instance method.

Static method does not Instance method depends on depend on the single object the object for which it is because it belongs to a class. available.

Static method can be Instance method cannot be invoked without creating an invoked without creating an object, by using class name. object.

ClassName.method(

);

( );ObjectName.method

Static methods cannot call Non-static methods can call non-static methods. static methods. Static methods cannot Non-static methods access not-static variables. access static variables. can

Static methods cannot refer Instance methods can refer to this or super. to this and super.

Static Blocks
There are also static blocks of code in java, which have the following syntax:
static { statements; }

These blocks are used to initialize the variables and execute at class load time.

static import

static import J2SE 5.0 introduces a new feature called static import that lets you import the static variable of a class so that you can refer to them without having to mention the class name.
import static java.lang.Math.PI; import static java.lang.System.out;

Methods
with variable length parameters

Method with variable length parameters J2SE 5.0 introduces a new feature that lets you define methods with a variable number of parameters, so that you can make several method calls with a variable number of arguments. These methods are called variable-length argument methods.

Syntax The variable-length parameters list consists of a type followed by three dots and the variable name.
public void print(int... values) { for(int i: values) { System.out.println(i); } }

Note There must be only one variable parameter list in the method. Following method is invalid length

public void print(int... a, int... b) { }

Note If there are individual parameters in addition to the list, the variable length parameters list must appear last inside the parameters of the method. Following method is valid:
public void print(String s, int... b) { }

Method Overloading

Method Overloading
In java, it is possible to define two or more methods with the same name, within the same class. They share the same name as long as their parameters are different. This process overloading. is known as method

When java call an overloaded method, it simply executes the version of the method whose parameters match the arguments. Two methods are overloaded if there: 1. No of parameters are different 2. Type of parameters is different 3. Order of parameters is different

No of parameters are different. 1


public void add(int i) { System.out.println(i+i); } public void add(int i, int j) { System.out.println(i+j); } public void add(int i, int j, int k) { System.out.println(i+j+k); }

Type of parameters is different. 2


public void add(int i, int j) { System.out.println(i+j); } public void add(double i, double j) { System.out.println(i+j); } public void add(double i, int j) { System.out.println(i+j); }

Order of parameters is different. 3


public void add(double i, int j) { System.out.println(i+j); }

public void add(int i, double j) { System.out.println(i+j); }

You might also like