Home >Java >Javagetting Started >what are java inner classes

what are java inner classes

(*-*)浩
(*-*)浩Original
2019-11-09 11:24:202885browse

When a class is defined inside another class, the class is called an inner class.

what are java inner classes

Common characteristics of internal classes (Recommended learning: java course )

(1),The inner class is still an independent class. After compilation, the inner class will be compiled into an independent .class file, but it will be preceded by the class name of the external class and the $ symbol.

(2). Internal classes cannot be accessed in ordinary ways. The inner class is a member of the outer class, so the inner class can freely access the member variables of the outer class, whether private or not.

(3) If the inner class is declared static, it cannot access the member variables of the outer class casually. At this time, the inner class can only access the static member variables of the outer class.

Member inner class

class Outer {
    class Inner{}
}

Compiling the above code will produce two files: Outer.class and Outer$Inner.class.

Method inner class

Put the class inside the method

class Outer {
    public void doSomething(){
        class Inner{
            public void seeOuter(){
            }
        }
    }
}

The above is the detailed content of what are java inner classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is a package in javaNext article:what is a package in java