• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Inner classes, Method-Local Inner classes and Anonymous Classes

October 6, 2011 By j2eereference Leave a Comment

Inner classes, Method-Local Inner classes and Anonymous Classes

Inner Classes

Inner class is a member of an outer class. Inner class can access members of outer class. Inner class can also access private member of outer class.

Defining Inner class

Inner class are defined within the braces of outer class

Ex:

class Outer {

class Inner { }

}

Modifiers applied to Inner class are :

Private, Public, Protected

Final, Abstract, Strctfp

Instantiating an Inner class

Inner class can be accessed through the instance of outer class. To create an instance of inner class, instance of outer class must be created.

Ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Outer {
 
    private  int i= 9;
 
    class Inner
     {
        public void innerMethod()
         {
           System.out.println(“I is : “ +i);
         }
     }
 
    public static void main(String[] args)
     {
      Outer.Inner in = new Outer().new Inner();
      in.innerMethod();
     }
}

Output : I is 9.

 

Method-Local Inner Class

Inner class defined inside the method of any class called Method-Local Inner class.  It can be instantiated only within the method where the Inner class is defined. Inner class defined inside the method must create an instance within the method but below the inner class definition.

Ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Outer
{
 
   private int x = 6;
 
   void outerMethod()
   {
      class Inner
       {
          public void innerMethod()
           {
              System.out.println(“X is: “ +x);
           }
       }
 
   Inner in = new Inner();
   in.innerMethod();
 
   }
}

 

Modifiers applied to Method-Local Inner Class are  Abstract and Final

Anonymous Inner Class

We can declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes.

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.