Java Local Classes
In this chapter you will learn:
- How to define a local class
- Example - Java Local Classes
- How to create a local class and return it from a method
Description
A local class is a class that is declared anywhere that a local variable is declared. A local class has the same scope as a local variable.
A local class has a name and can be reused. A local class instance can access the surrounding scope's local final variables and final parameters.
Example
Java Local Class
class MyClass {/* ww w . ja v a 2s.c o m*/
void myMethod(final int x) {
final int y = x;
class LocalClass {
int a = x;
int b = y;
}
LocalClass lc = new LocalClass();
System.out.println(lc.a);
System.out.println(lc.b);
}
}
public class Main {
public static void main(String[] args) {
MyClass ec = new MyClass();
ec.myMethod(10);
}
}
The code above generates the following result.
Example 2
The following code declares an Iterator interface and Iter inner class.
class Item{/*from w w w . ja v a 2 s.c om*/
private String name;
private String value;
public Item(String n, String v){
name = n;
value = v;
}
public String toString(){
return name + value;
}
}
interface Iterator {
boolean hasMoreElements();
Object nextElement();
}
class ItemManager {
private Item[] itemArray;
private int index = 0;
ItemManager(int size) {
itemArray = new Item[size];
}
Iterator iterator() {
class Iter implements Iterator {
int index = 0;
@Override
public boolean hasMoreElements() {
return index < itemArray.length;
}
@Override
public Object nextElement() {
return itemArray[index++];
}
}
return new Iter();
}
void add(Item item) {
itemArray[index++] = item;
}
}
public class Main {
public static void main(String[] args) {
ItemManager itemManager = new ItemManager(5);
itemManager.add(new Item("#1", "A"));
itemManager.add(new Item("#2", "B"));
itemManager.add(new Item("#3", "C"));
Iterator iter = itemManager.iterator();
while (iter.hasMoreElements()){
System.out.println(iter.nextElement());
}
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to declare Nonstatic Member Classes
- Example - Java Member Classes
- How to use inner class as the underline data structure
- What is variable scope in terms of inner class
- Example - The inner class members are accessible only within the inner class
Java Class
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member ClassesJava Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files