GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||
GeezerVison is showing jenkov.com — Java Nested Classes Java Language
Java Nested Classes
In Java nested classes are classes that are defined inside another class. The purpose of a nested class is to clearly group the nested class with its surrounding class, signaling that these two classes are to be used together. Or perhaps that the nested class is only to be used from inside its enclosing (owning) class. Java developers often refer to nested classes as inner classes, but inner classes (non-static nested classes) are only one out of several different types of nested classes in Java.
In Java nested classes are considered members of their enclosing class. Thus, a nested class
can be declared You can create several different types of nested classes in Java. The different Java nested class types are:
All these types of nested classes will be covered in the following sections. Static Nested ClassesStatic nested classes are declared in Java like this: public class Outer {
public static class Nested {
}
}
In order to create an instance of the Outer.Nested instance = new Outer.Nested(); In Java a static nested class is essentially a normal class that has just been nested inside another class. Being static, a static nested class can only access instance variables of the enclosing class via a reference to an instance of the enclosing class. Non-static Nested Classes (Inner Classes)Non-static nested classes in Java are also called inner classes. Inner classes are associated with an instance of the enclosing class. Thus, you must first create an instance of the enclosing class to create an instance of an inner class. Here is an example inner class definition: public class Outer {
public class Inner {
}
}
Here is how you create an instance of the Outer outer = new Outer(); Outer.Inner inner = outer.new Inner();
Notice how you put Non-static nested classes (inner classes) have access to the fields of the enclosing class, even if they are declared private. Here is an example of that: public class Outer {
private String text = "I am private!";
public class Inner {
public void printText() {
System.out.println(text);
}
}
}
Notice how the Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.printText(); Inner Class ShadowingIf a Java inner class declares fields or methods with the same names as field or methods in its enclosing class, the inner fields or methods are said to shadow over the outer fields or methods. Here is an example: public class Outer {
private String text = "I am Outer private!";
public class Inner {
private String text = "I am Inner private";
public void printText() {
System.out.println(text);
}
}
}
In the above example both the
Java makes it possible though, for the public class Outer {
private String text = "I am Outer private!";
public class Inner {
private String text = "I am Inner private";
public void printText() {
System.out.println(text);
System.out.println(Outer.this.text);
}
}
}
Now the Local Classes
Local classes in Java are like inner classes (non-static nested classes) that are defined inside a method or scope block ( class Outer {
public void printText() {
class Local {
}
Local local = new Local();
}
}
Local classes can only be accessed from inside the method or scope block in which they are defined. Local classes can access members (fields and methods) of its enclosing class just like regular inner classes.
Local classes can also access local variables inside the same method or scope block, provided
these variables are declared
From Java 8 local classes can also access local variables and parameters of the method the local class is declared in.
The parameter will have to be declared
Local classes can also be declared inside static methods. In that case the local class
only has access to the static parts of the enclosing class. Local classes cannot contain all kinds of static
declarations (constants are allowed - variables declared The same shadowing rules apply for local classes as for inner classes. Anonymous ClassesAnonymous classes in Java are nested classes without a class name. They are typically declared as either subclasses of an existing class, or as implementations of some interface.
Anonymous classes are defined when they are instantiated. Here is an example that declares an anonymous
subclass of a superclass called public class SuperClass {
public void doIt() {
System.out.println("SuperClass doIt()");
}
}
SuperClass instance = new SuperClass() {
public void doIt() {
System.out.println("Anonymous class doIt()");
}
};
instance.doIt();
Running this Java code would result in A Java anonymous class can also implement an interface instead of extending a class. Here an example: public interface MyInterface {
public void doIt();
}
MyInterface instance = new MyInterface() {
public void doIt() {
System.out.println("Anonymous class doIt()");
}
};
instance.doIt();
As you can see, an anonymous class implementing an interface is pretty similar to an anonymous class extending another class. An anonymous class can access members of the enclosing class. It can also access local variables which are declared final or effectively final (since Java 8). You can declare fields and methods inside an anonymous class, but you cannot declare a constructor. You can declare a static initializer for the anonymous class instead, though. Here is an example: final Strint textToPrint = "Text...";
MyInterface instance = new MyInterface() {
private String text;
//static initializer
{ this.text = textToPrint; }
public void doIt() {
System.out.println(this.text);
}
};
instance.doIt();
The same shadowing rules apply to anonymous classes as to inner classes. Nested Class BenefitsThe benefits of Java nested classes are that you can group classes together that belong together. You could do so already by putting them in the same package, but putting one class inside another makes an even stronger grouping. A nested class is typically only used by or with its enclosing class. Sometimes a nested class is only visible to the enclosing class, is only used internally, and is thus never visible outside the enclosing class. Other times the nested class is visible outside its enclosing class, but can only be used in conjunction with the enclosing class.
An example would be a
Here are two public class Cache {
private Map<String, CacheEntry> cacheMap = new HashMap<String, CacheEntry>();
private class CacheEntry {
public long timeInserted = 0;
public object value = null;
}
public void store(String key, Object value){
CacheEntry entry = new CacheEntry();
entry.value = value;
entry.timeInserted = System.currentTimeMillis();
this.cacheMap.put(key, entry);
}
public Object get(String key) {
CacheEntry entry = this.cacheMap.get(key);
if(entry == null) return null;
return entry.value;
}
}
public class Cache {
private Map<String, CacheEntry> cacheMap = new HashMap<String, CacheEntry>();
public class CacheEntry {
public long timeInserted = 0;
public object value = null;
}
public void store(String key, Object value){
CacheEntry entry = new CacheEntry();
entry.value = value;
entry.timeInserted = System.currentTimeMillis();
this.cacheMap.put(key, entry);
}
public Object get(String key) {
CacheEntry entry = this.cacheMap.get(key);
if(entry == null) return null;
return entry.value;
}
public CacheEntry getCacheEntry(String key) {
return this.cacheMap.get(key);
}
}
The first Next: Java Record
Close TOC
All Trails Trail TOC Page TOC Previous Next | ||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||