Nested class

Refer to Java - Inner classes

Writing a class within another class is allowed in java. The class written within is called a nested class, and the class holding the inner class is called the outer class.

Nested class
  • Non-static nested class
    • Inner class
    • Method-local inner class
    • Anonymous inner class
  • Static nested class
Inner class
class OuterDemo01 {

    private class InnerDemo {
        private void print() {
            System.out.println("This is an inner class.");
        }
    }

    void displayInner() {
        InnerDemo inner = new InnerDemo();
        inner.print();
    }
}
public class TestNested {
    public static void main(String[] args) {
        OuterDemo01 outer = new OuterDemo01();
        outer.displayInner();
    }
}
This is an inner class.

class OuterDemo02 {
    private int num = 10;

    public class InnerDemo {
        public int getNum() {
            return num;
        }
    }
}
public class TestNested02 {
    public static void main(String[] args) {
        OuterDemo02 outer = new OuterDemo02();
        OuterDemo02.InnerDemo inner = outer.new InnerDemo();
        System.out.println(inner.getNum());
    }
}
10
Method-local inner class
public class OuterDemo03 {

    private void myMethod() {
        int num = 10;

        class MethodLocalDemo {
            private void print() {
                System.out.println("This is a method-local inner class " + num);
            }
        }

        MethodLocalDemo methodLocal =  new MethodLocalDemo();
        methodLocal.print();
    }

    public static void main(String[] args) {
        OuterDemo03 outer = new OuterDemo03();
        outer.myMethod();
    }
}
This is a method-local inner class 10
Anonymous inner class

An inner class declared without a class name is known as an anonymous class. In case of anonymous inner classes, we declare and instantiate them at the same time.
Generally they are used to override the methods of a class or an interface.

abstract public class AnonymousInner {
    public abstract void myMethod();
}
public class OuterDemo04 {
    public static void main(String[] args) {
        AnonymousInner inner = new AnonymousInner() {
            @Override
            public void myMethod() {
                System.out.println("This is an example of an anonymous inner class.");
            }
        };
        inner.myMethod();
    }
}
This is an example of an anonymous inner class.

Generally, if a method accepts an object of an interface, an abstract class, or a concrete class, then we can implement the interface, extend the abstract class, and pass the object to the method. If it is a class, then we can directly pass it to the method.

interface Message {
    String greet();
}
public class ArgumentClassDemo {

    public void displayMessage(Message message) {
        System.out.println(message.greet() + ", " + "this is an example of an anonymous inner class as an argument.");
    }

    public static void main(String[] args) {
        ArgumentClassDemo argumentDemo = new ArgumentClassDemo();
        argumentDemo.displayMessage(new Message() {
            @Override
            public String greet() {
                return "Hello";
            }
        });
    }
}
Hello, this is an example of an anonymous inner class as an argument.
Static nested class

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static member.Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

public class OuterDemo05 {

    static class NestedDemo {
        void show() {
            System.out.println("This is the method from my static nested class.");
        }
    }

    public static void main(String[] args) {
        OuterDemo05.NestedDemo nested = new OuterDemo05.NestedDemo();
        nested.show();
    }
}
This is the method from my static nested class.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • 人有着无数的诱惑,也有无数的苦恼,克服所有诱惑和苦恼,唯有一个正确的价值观,和自愈的能力,它们虽然不能让我们避免痛...
    燕Carol阅读 462评论 1 4
  • 养鸡场的山崖边立了个牌子上面写着励志鸡汤文:“亲,你不勇敢地飞下来,怎么会知道自己原来是一只可以搏击长空的雄鹰?”...
    vikinganli阅读 778评论 0 0
  • 曾写过《为谁流向潇湘去》,郴州那一游,该是向依赖性心理的一次告别,毋庸置疑那之前,会受到种种过去与未来的牵绊...
    如来阅读 337评论 1 4
  • 曾经有一位著名经济学家,在他年轻的时候考取研究生,一次就被成功录取。后来他问导师,当时是哪一点打动了对方,导师伸手...
    Ryan闲说阅读 346评论 0 1