My previous
post was on Overloading and argument passing in Java. Today in this post
I will explain nested and inner class concepts of Java. Before taking on the
nested and inner class concepts I would like to brief you on the Java’s
keywords, public, private and static.
Note: The
Instance-variables and methods, both together are referred as class members.
Public Keyword: When a public keyword is specified on
any class member then that class member can be accessed directly by any code
within the class and also outside the class
by creating instance of that class.
The below code fragment shows you the
declaration of public variables and methods.
public int
a=20; // public variable
public
char ch; //public variable
public int additionMethod (int a, int b) // public
method
{
return a+b;
}
Public variables and methods are
declared by using keyword public as
shown in above code fragment.
Private Keyword: When private keyword is specified on
any class member then that class member can only be accessed within its class
and not possible to access it outside the class.
It possible to get and set
the private variables values only if the class containing the private variables
contains getter and setter methods.
The below example show you,
how to declare a private variables and methods. It also shows the use of getter
and setter methods.
package sample.example1627;
public class SamplePrivateKeyword
{
private int a=20;
int b;
private int additionMethod(int x,int y)
{
return a+b;
}
void invokeAdditionMethod()
{
int s=additionMethod(20, 30);
System.out.println();
System.out.println("value returned by
addtionMethod is "+s);
System.out.println();
}
public int getA()
{
return a;
}
public void setA(int a)
{
this.a = a;
}
}
package sample.example1627;
public class DemoSamplePrivateKeyword
{
public static void main(String[] args)
{
SamplePrivateKeyword sample1=new SamplePrivateKeyword();
//sample1.a; // a is not visible because it is private
sample1.b=20;
//sample1.additionMethod(2,4); is not
visible because method is private
// Using getter and setter
int getvar=sample1.getA(); //implemented getter
method
System.out.println("get value by
getter method "+getvar);
sample1.setA(50);// implemented setter
method
//after setting private variable a
System.out.println("value of private
variable a, after using setter method "+sample1.getA());
sample1.invokeAdditionMethod();
}
}
Output:
get value by getter method 20
value of private variable a, after using setter
method 50
value returned by addtionMethod is 70
In the above example program
you can clear see how private keyword is essential in hiding data of your class
from other classes. You can also see in the output how the private variable
value is assessed by getter method and value of private variable ‘a’ is set
using setter methods.
Note: Class
and interfaces cannot be private.
Static Keyword: Static keyword is used to create
class members that can be accessed by itself without can requirement of object
reference. Static class members can be accessed before the creation of any
instance of the class.
Static keyword is used in
Java, in three circumstances
1. To create Static Variables
2. To create Static Method
3. To create static block of codes.
Static Variables: Static variables are declared using static keyword. See code fragment below
showing the static variable declaration.
static
int a=10;
Some important
facts to remember:
a) Static variables are initialized only
once at the beginning of the execution.
b) Static variables can be used before
any instance of class is created.
c) Static variables can be accessed
directly without using any instance of class by using only class name.
Static Methods: The methods are defined with the static keyword. The below mentioned are
the restrictions on static methods.
a) Static method can only call other
static methods.
b) Static methods can only access static
data.
c) Static method cannot refer to ‘this’ or ‘super’ keywords.
Static Block: A block in java that is used to
initialize the static data members of a class. When class will be first loaded
to JVM, static block will be executed. The static block syntax is shown be in
code fragment below.
static {
// body of block
}
The example program shows you how static keyword is implemented in java. All the three circumstance
in which static keywords is used is illustrated in the program example below.
package sample.example1627;
public class SampleStaticKeyword
{
static int a;
static int b;
static void Addition(int a,int b)// static method
{
System.out.println("Addition result
"+(a+b));
}
static // static block
{
System.out.println("am static
block");
a=10;
b=20;
System.out.println("a & b static
variable initialized in static block, "+a+" "+b);
}
}
package sample.example1627;
public class DemoSampleStaticKeyword
{
public static void main(String[] args)
{
//to access static method object is not required
SampleStaticKeyword.Addition(16, 27);
}
}
Output:
am static block
a & b static variable initialized in static
block, 10 20
Addition result 43
In above example program you can see that
static method code is placed before the static block code. But in the output you
can see that static block is executed first. This is because static block will
be executed first when the class is first loaded to JVM.
Nested and Inner Classes in Java:
A class defined within another class is called
nested class. The scope of nested class is defined by its’ enclosing class.
A nested class can
directly access all the class members include private members of its’ enclosing
class. But enclosing class cannot access the class members of the nested class directly.
In order to access it you should create instance of the nested class.
There are two types of nested
class:
1. Static
2. Non-static
Static nested class is the one with static modifier applied to it.
It cannot access enclosing class members directly. In order to access it,
instance of enclosing class has to be created. Static nested classes are not
often used.
Non-static nested class is important nested class which is very often
used. Inner class is a non-static
nested class
.
An inner class can access all
the class members of its’ enclosing class directly, even the private members of
its’ enclosing class.
The below example program
shows the implementation of inner class in java
package sample.example1627;
public class SampleInnerClass
{
int a=10;
private int b;
class InnerClass
{
int c=20;
void addtion(int a,int b)// inner class method
{
System.out.println("addition result in
inner class "+(a+b));
}
//java block, creating a scope.
// you can see that inner class can access class
members of enclosing class directly.
{
b=20;// initializing private
variable of enclosing class.
System.out.println("value of variable
a&b of enclosing class "+a+" "+b);
}
}
void invokeInnerMethod()
{
InnerClass inner=new InnerClass();
inner.addtion(20, 30);
}
public static void main(String[] args)
{
SampleInnerClass sample1=new SampleInnerClass();
sample1.invokeInnerMethod();
}
}
Output:
value of variable a&b of enclosing class 10 20
addition result in inner class
50
Note: The scope of inner class is delimited
by the scope of its’ enclosing class. Therefore accessing of inner class
outside its’ enclosing class is not possible directly. In order to access you
have refer the inner class by its enclosing class. The following code fragment
shows how it is implemented, enclosingClassname.innerClassname.