Inner classes & anonymous in java

Its block inside a class & behave as member of class.It doesn't just under package but under class of a package.Inner class have access to all members of outer class.

class class{
           inner class{
                 }
}

It not required to make an instance of an inner class in order to have an instance of the outer class but to get instance of inner class we need to create instance of outer class & via it we can create instance of inner class.



Inner class type

Static ( inner class having static modifer)
Member inner class ( normal class inside class)
Method Local ( inner class defined inside a method  of class)
Anonymous  ( inner class having no name)

  

1. Member inner class

From inside outer class inner class can be access like normal class see example as below.


//outer class declaration
class MyOuterClass {
private int i = 18;

// Creating instance of inner class and calling inner class function


public void createInnerInstance() {
// access inner class as normal if you are under scope of outer class
MyInnerClass inn1 = new MyInnerClass();  
inn1.getValue();
}

// inner class declarataion
class MyInnerClass {
public void getValue() {
// accessing private variable from outer class
System.out.println("value of i -" + i);
}
}



From outside of outer class function create instance of outer class then create instance of inner class to access any of its method. you can see that in below example.

class Test{
public static void main(string args[]){

MyOuterclass myouterclass = new MyOuterclass();
MyOuterclass.MyInnerclass  myinnerclass = MyOuterclass().new MyInnerclass ();

myinnerclass .getValue();

}}



this” keyword will refer the curren executing inner class Object. But to get this for outer class will need to use “OuterClass.this”.



2. When Inner class is inside method of class rather then class itself its called Method local inner class. Final & Abstract are the only modifiers available for this type of inner class.




class MyOuterClass {
private int i = 18;

public void methodOuterclass(){

// inner class declarataion
class MyInnerClass {
public void getValue() {
System.out.println("value of i -" + i);
}

//inner class instance creation
InnerClass localinner1 = new InnerClass();
localinner1.getValue();
}
 }



scope of the inner class is restricted within the method.
Method local inner class can not use the local variable defined in method While it can use the instance variable



When inner class don't have name its called anonymous inner class. as well as Inner classes having Static modifier are known as Static inner class.


static inner class cannot access non static member of outer class. Due to static in nature it can be created without creating instance of outer class like
MyOuter.MyInner i = new MyOuter.MyInner(); 

inner class cannot declare any static members unless they are compile-time constants and are primitives or Strings.




3. Anonymous inner class


public class AnonymousClassDemo
{
  public static void main(String[] args)
  {
    Fruit fruit= new Fruit() {
      public void someFruit ()
      {
        System.out.println("Anonymous Fruit");
      }   
    }; // anonymous class body closes here
    //fruit contains an object of anonymous subclass of Fruit.
    fruit.someFruit();
  }
}
 
class Fruit
{
  public void someFruit()
  {
    System.out.println("Organic Fruit");
  }
}


This is the functionality of defining an anonymous inner class because we want to override one or more methods of the super class on the fly.
anonymous inner classes can either implement an interface or extend a class, they can't do both at the same time. There is no such mechanism by which an anonymous inner class can implement multiple interfaces



4. Static Inner classes or Nested Static class

This is same as the Normal regular Innerclass, but it has the static members available .
Only static members of Outer class is directly accessible in nested static class. If we want to create the Inner Class Object no need to depend on the Outer Class Object


class Test{
public static void main(string args[]){

MyOuterclass myouterclass = new MyOuterclass(); Not required Here

//see the difference where earlier we have intsntiated normal member inner class.
MyOuterclass.StaticInnerclass  myStaticInnerclass = new StaticInnerclass.MyInnerclass ();
myStaticInnerclass.getValue();

}}


Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.







Comments

  1. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  2. Nice tutorial about Java Inner Class .
    thanks for this nice post.

    ReplyDelete

Post a Comment

Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java