Methods: A method represents a group of statements to perform a
task. A method contains two parts: method header (or) method prototype is first
part. This part contains method name, method parameters and method returntype.
return_type
methodname (param1, param2, ……)
e.g.: double sum (double d1, double d2), void sum (), float
power (float x, int n), etc.
The second part contains method
body. This part represents the logic to perform the task in the form of a group
of statements.
{
Statements;
}
Note: If a method
returns a value then return statement should be written in method body.
e.g.: return x; return 1; return
(x+y-2);
· Instance
Methods:
o
Methods which act upon instance variables of a
class are called instance methods.
o
To call instance methods use
objectname.methodname.
o Instance
variable is a variable whose separate copy is available in every object.
o
Any modifications to instance variable in one
object will not affect the instance variable of other objects. These variables
are created on heap.
Program 1: Write a program
to access instance variable using instance method.
//instance
method accessing instance variable class
Sample
{ int x = 10;
void
display( )
{
x++;
System.out.println
(" x value is : " + x);
}
}
class SDemo
{ public
static void main(String args[])
{
Sample s1 = new Sample( );
System.out.print
(“s1 Object Contains : “);
s1.display
();
Sample
s2 = new Sample( );
System.out.print
(“s2 Object Contains : “);
s2.display
();
}
}
Output:
Note: Instance methods can read and act upon static variables also.
· Static
Methods:
o
Static methods can read and act upon static
variables.
o
Static methods cannot read and act upon instance
variables.
o
Static variable is a variable whose single copy
is shared by all the objects.
o
Static methods are declared using keyword
static.
o
Static methods can be called using
objectname.methodname (or) classname.methodname.
o
From any object, if static variable is modified
it affects all the objects. Static variables are stored on method area.
//static method accessing static
variable
class Sample
{ static
int x = 10; static void display( )
{
x++;
System.out.println
(" x value is = " + x);
}
}
class SDemo
{ public static void main(String
args[])
{
System.out.print (“Calling static
method using Class name : “);
Sample.display
();
Sample
s1 = new Sample ( );
System.out.print
(“Calling static method using Object name : “);
s1.display
();
}
}
Output:
Inner Class: A class with in another class is called Inner class.
When the programmer wants to restrict the access of entire code of a class,
creates an inner class as a private class. The way to access the inner class is
through its outer class only.
· Inner
class is a safety mechanism.
· Inner
class is hidden in outer class from other classes.
· Only
inner class can be private.
· An
object to Inner class can be created only in its outer class.
· An
object to Inner class cannot be created in any other class.
· Outer
class object and Inner class objects are created in separate memory locations.
· Outer
class members are available to Inner class object.
· Inner
class object will have an additional invisible field called ‘this$0’ that
stores a reference of outer class object.
· Inner
class members are referenced as: this.member;
· Outer
class members are referred as: Outerclass.this.member;
// inner class demo class Bank
{ private
double bal, rate;
Bank (double b, double r)
{
bal=b; rate = r;
}
void display ( )
{
Interest in=new Interest ();
in.calculateInterest ( );
System.out.println ("New Balance
: " + bal);
}
private
class Interest
{
void calculateInterest ( )
{
System.out.println ("Balance
= "+ bal);
double
interest=bal* rate/100;
System.out.println
("interest = "+interest);
bal+=interest;
}
}
}
class InnerDemo
{ public
static void main (String args[])
{ Bank
account = new Bank (20000, 5);
account.display
();
}
}
Output: