Professional Documents
Culture Documents
Method
A method is a group of programming language statements with a given name.
Method syntax
Returntype methodname (parameters) { statement 1; statement 2; .. }
Body
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.
example
)static void display(int a, int b { System.out.println(a+b); }
Class Methods
vs.
Instance Methods
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
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