Home Java Java-get-example Use of Local Inner class

Related Tutorials


 
 

Share on Google+Share on Google+

Use of Local Inner class

Advertisement
The Java language enables you to define a class inside another class. Such a class is defined as Nested Class or Local Inner class.

Use of Local Inner class

     

The Java language enables you to define a class inside another class. Such a class is defined as Nested Class or Local Inner class. The Nested Class are classified into -

1)Static Nested Inner class are declared static called static Nested classes.

2)Non-Static Nested classes are those classes that are not defined static. This class is defined also as Inner class.

The Inner class is defined as local Inner class when you declare a inner class within the body of method.

Understand with Example

The Tutorial illustrates an example from Local Inner class. The code include a Outer class Inner class include a method that add the integer into the array. The println print the even indices of the array. The inner class Get Even Integer is same as Java iterators,The work of Get Even Integer are used to step through a Inner class, This method  used in GetBig Integer is used to retrieve the last element, the current element and the last element. The main method that instantiate a Inner Class object and this is used to fill the array Of Ints array with the specified value, This call a print even method and print the required even output in ascending order.

Here is the code:

public class InnerClass {
private final static int size = 20;
private int[] arrayOfIntger = new int[size];

public InnerClass() {
for (int i = 0; i < size; i++) {
arrayOfIntger[i= i;
}
}
public void even() {
GetEvenInteger integer = this.new GetEvenInteger();
while (integer.hasNext()) {
System.out.println(integer.getNext() " ");
}
}
private class GetEvenInteger {
private int next = 0;
public boolean hasNext() {
return (next <= size - 1);
}
public int getNext() {
int value = arrayOfIntger[next];
next += 2;
return value;
}
}
public static void main(String args[]) {
InnerClass innerClass = new InnerClass();
System.out.println("The Even integers are: ");
innerClass.even();
}
}

Output will be displayed as:

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 22, 2008

Related Tutorials

Discuss: Use of Local Inner class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com