The difference between a Java inner class and an external class

Source: Internet
Author: User

Recently, while looking at Java-related knowledge, we found that there are both internal and non-public class concepts in Java, and that these two classes can share a single file with other classes without the need for separate file writing. According to the individual summary of the similarities and differences between the two summarized as follows, if there is any inappropriate place, welcome to correct me.

1. The file name corresponding to the non-public class and the inner class is independent of the class name of these two classes;

2, a source file can contain multiple non-public classes or internal classes;

3, non-public class can not use the Common keyword (generally before the keyword), the inner class may use public, private, protected keywords;

4, non-public class can add 0 to more internal classes;

5, the non-public class is the package is visible , a package can only have a non-public class with the same name, so the non-public class cannot be called outside the package, but outside the package can be called in the package of foreign class objects in the internal classes .

6, non-public classes are called directly in the source file, for example, there is a public class test in the source file Test.java, a non-public class pritest, you can use pretest pt = new Pretest () to instantiate a non-public class object, and for the inner class, Outside a non-static method, you must use an object of the outer class to invoke the constructor of the inner class, such as an external class Outer in the source file Outer.java, an inner class inner, and call the inner class using the following method:Outer out = new Outer (), Inner in = Out.new Inner (). The inner class object is instantiated directly inside a non-static method using the new class, which is Inner in = new Inner ().

7. Non-public classes can be used only by creating public class objects for the use of member variables of the public class, while inner classes are free to use the member variables of the outer class (including private variables) without generating an object of the outer class.

8. If there is a public class and multiple non-public classes in the source file, At compile time, the compiler creates a separate. class file for each class in the file, and the file name is the corresponding class name (so there can only be one non-public class with the same name in a package); If there is an external class, multiple internal classes in a file, A. class file is created for each class at compile time, but the file name For the "outer class $ inner class.class", the $ represents the external class to which this inner class belongs.

9. There are three forms of accessing variables in the inner class:
A, variable name--access to local variables;
b, this. Variable name--access to internal class variables;
C, external class name. This variable name--accesses the external class variable.

If the inner class does not have a member variable and a local variable with the same name, the inner class can access the outer class variable directly from the variable name without specifying the external class name. this. Variable name.

10, if an inner class is a static inner class, using the keyword static, the inner class can only access static member variables of the outer class, and the "external class name. Inner class name" can be regarded as a whole , and the object of the inner class is directly new, without the use of the external class object. For example, if the outer class is out and the static inner class is in, you can create an inner class object directly using out.in in = new out.in () .

11. If an inner class is "private", the class can only generate objects in the outer class , and no longer use out.in in = new Out (). New in () generates an inner class object.

The difference between a Java inner class and an external class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.