3 method
TRANSCRIPT
-
7/28/2019 3 Method
1/26
Methods
-
7/28/2019 3 Method
2/26
Method
A method is a group of programming
language statements with a given name.
Method syntaxReturntype methodname (parameters)
{
statement 1;
statement 2;
..
}
-
7/28/2019 3 Method
3/26
void sum ( int a, int b )
{
statement 1
statement 2..
}
Declaration
Body
Method has two major parts:
1. Method Declaration
2. Method Body
-
7/28/2019 3 Method
4/26
Method that does not return a value
If a method does not return a value, itsreturn type must be declared void.
For example:
voidabc( )
{System.out.println(Hello World);
}
-
7/28/2019 3 Method
5/26
Method that returns a value
Methods that have a return type otherthan void return a value to the calling
routine using the following form of the
return statement.
return value;
int abc( ){
return 5+5;
}
-
7/28/2019 3 Method
6/26
Class
Methods(static methods)
-
7/28/2019 3 Method
7/26
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.
Creating Static Methods
-
7/28/2019 3 Method
8/26
static Returntype methodname (parameters)
{statement 1;
statement 2;
..
}
Static Method Syntax
staticvoiddisplay(int a, int b)
{
System.out.println(a+b);
}
example
-
7/28/2019 3 Method
9/26
Class Methodsvs.
Instance Methods
-
7/28/2019 3 Method
10/26
All objects have their own
copy of instance method.
All objects share the single
copy of static method.
instance methods are
declared without statickeyword.
Static methods are declared
by using static keyword.
Instance MethodStatic Method
Static vs. Instance Methods
-
7/28/2019 3 Method
11/26
Instance method cannot be
invoked without creating an
object.
ObjectName.method( );
Static method can be
invoked without creating an
object, by using class name.
ClassName.method( );
Instance method depends on
the object for which it is
available.
Static method does not
depend on the single object
because it belongs to a class.
-
7/28/2019 3 Method
12/26
Instance methods can refer
tothisand super.
Static methods cannot refer
to this orsuper.
Non-static methods canaccess static variables.
Static methods cannotaccess not-static variables.
Non-static methods can call
static methods.
Static methods cannot call
non-static methods.
-
7/28/2019 3 Method
13/26
There are also static blocks of code in java,
which have the following syntax:
static
{statements;
}
These blocks are used to initialize thevariables and execute at class load time.
Static Blocks
-
7/28/2019 3 Method
14/26
static
import
-
7/28/2019 3 Method
15/26
static import
J2SE 5.0 introduces a new feature calledstatic import that lets you import the static
variable of a class so that you can refer to
them without having to mention the classname.
import static java.lang.Math.PI;
import static java.lang.System.out;
-
7/28/2019 3 Method
16/26
Methodswith variable length
parameters
-
7/28/2019 3 Method
17/26
Method with variable length parameters
J2SE 5.0 introduces a new feature that letsyou 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-lengthargument methods.
-
7/28/2019 3 Method
18/26
Syntax
The variable-length parameters list consistsof a type followed by three dots and the
variable name.
public voidprint(int... values)
{
for(int i: values)
{
System.out.println(i);}
}
-
7/28/2019 3 Method
19/26
Note
There must be only one variable lengthparameter list in the method.
Following method is invalid
public voidprint(int... a, int... b)
{
}
-
7/28/2019 3 Method
20/26
Note
If there are individual parameters in additionto the list, the variable length parameters list
must appear last inside the parameters of the
method.
Following method is valid:
public voidprint(String s, int... b){
}
-
7/28/2019 3 Method
21/26
Method
Overloading
-
7/28/2019 3 Method
22/26
Method Overloading
In java, it is possible to define two or moremethods with the same name, within the
same class.
They share the same name as long as their
parameters are different.
This process is known as methodoverloading.
-
7/28/2019 3 Method
23/26
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 different2. Type of parameters is different
3. Order of parameters is different
-
7/28/2019 3 Method
24/26
1.No of parameters are different
publicvoidadd(int i)
{
System.out.println(i+i);
}
publicvoidadd(int i, int j)
{
System.out.println(i+j);
}
publicvoidadd(int i, int j, int k){
System.out.println(i+j+k);
}
-
7/28/2019 3 Method
25/26
2.Type of parameters is different
publicvoidadd(int i, int j)
{
System.out.println(i+j);
}
publicvoidadd(double i, double j)
{
System.out.println(i+j);
}
publicvoidadd(double i, int j){
System.out.println(i+j);
}
-
7/28/2019 3 Method
26/26
3.Order of parameters is different
publicvoidadd(double i, int j)
{
System.out.println(i+j);
}
publicvoidadd(int i, double j)
{
System.out.println(i+j);
}