Java Inner Class

Java Inner Class

An inner class is a class that is defined inside another class.
The advantages of inner class:
• It is a way of logically grouping classes that are only used in one place, it is a name-hiding and code organization scheme, Inner classes can be hidden from other classes in the same package.
• It increases encapsulation.
• Nested classes can lead to more readable and maintainable code.
• Inner class methods can access the data from the scope in which they are defined, including data that would otherwise be private.
• Anonymous inner classes are handy when you want to define callbacks without writing a lot of code.

The Inner Classes can be classified in four types:
Regular Inner Class or Instance Inner Class
The regular inner class is a full fledged member of the outer class, so it shares all the features of a member of a class. It is associated with an instance of its enclosing class and has direct access to that object's all methods and fields including private methods and fields.
Because an inner class is associated with an instance, it cannot define any static members itself.
Static methods can only be declared in a static or top level type.

An object of inner class has a link to the enclosing object that made it,
An instance of InnerClass can exist only within an instance of OuterClass, to instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object like below:
OuterClass.InnerClass innerObject = InnerClass();

If you need to produce the reference to the outer-class object, you name the outer class
followed by a dot and this: OuterClass.this
Local Inner class
Local Inner class is created within a method or even an arbitrary scope.
Local inner classes are never declared with an access specifier (that is, public or private). Their scope is always restricted to the block in which they are declared.

Method Local Inner class is completely hidden from the outside world-not even outerclass.

They can not only access the fields of their outer classes, they can even access local variables! However, those local variables must be declared final.

final int[] counter = new int[1];
for (int i = 0; i < dates.length; i++)
dates[i] = new Date()
public int compareTo(Date other)
return super.compareTo(other);
The array variable is still declared as final, but that merely means that you can’t have it refer to a different array. You are free to mutate the array elements.
Anonymous Inner Classes
If you want to make only a single object of inner class, you don’t even need to give the class a name. Such a class is called an anonymous inner class.

In general, the syntax is
new SuperType(construction parameters)
inner class methods and data
SuperType can be an interface, then the inner class implements that interface. Or SuperType can be a class; then, the inner class extends that class.

An anonymous inner class cannot have constructors because the name of a constructor must be the same as the name of a class, and the class has no name. Instead, the construction parameters are given to the superclass constructor.

Anonymous inner class always extend a class or implement an interface, but not both. And if you do implement an interface, you can only implement one.
Static Inner Classes - Nested classes
If you don’t need a connection between the inner-class object and the outerclass object, then you can make the inner class static.

Only inner classes can be declared static. A static inner class is exactly like any other inner class, except that an object of a static inner class does not have a reference to the outer class object that generated it.

A static inner classs is analogous to a static method, it can only access static field and method of outer class, and can not access non-static outer-class fields or methods.
A static inner class can have static fields and methods.
Classes inside interfaces
Normally, you can’t put any code inside an interface, but a nested class can be part of an

Difference between outer class and Inner class
Outer classes can only be declared public or package private, a nested class can be declared private, public, protected, or package private and static.
Difference with C++ nested class
C++ has nested classes, the nesting is a relationship between classes, not objects.
The Java inner classes have an additional feature that makes them richer and more useful than nested classes in C++. An object that comes from an inner class has an implicit reference to the outer class object that instantiated it. Through this pointer, it gains access to the total state of the outer object.
In Java, static inner classes do not have this added pointer. They are the Java analog to nested classes in C++.

Core Java(TM), Volume I--Fundamentals (8th Edition) Ch.6 Interfaces and Inner Classes
Thinking in Java (4th Edition) Ch.12 Thinking in Java (4th Edition)
Nested Classes
Types of Inner Class in Java
Post a Comment


Java (159) Lucene-Solr (110) All (60) Interview (59) J2SE (53) Algorithm (37) Eclipse (35) Soft Skills (35) Code Example (31) Linux (26) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (15) Defects (14) Text Mining (14) J2EE (13) Network (13) PowerShell (11) Chrome (9) Continuous Integration (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Design (8) Dynamic Languages (8) Http Client (8) Maven (8) Security (8) Trouble Shooting (8) bat (8) blogger (8) Big Data (7) Google (7) Guava (7) JSON (7) Problem Solving (7) ANT (6) Coding Skills (6) Database (6) Scala (6) Shell (6) css (6) Algorithm Series (5) Cache (5) IDE (5) Lesson Learned (5) Miscs (5) Programmer Skills (5) System Design (5) Tips (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) OpenNLP (4) Project Managment (4) Python (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Firefox (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Google Drive (2) Gson (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Bit Operation (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Troubleshooting (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts