Java: Getting started from scratch, learning notes basics & lt; internal classes & gt; (14)

Source: Internet
Author: User

Internal class: the class defined in a class is called an internal class. One or more internal classes can be defined in a class, for example: in Class A, two classes are defined: class B and class C. Class A --> external class B and class C --> internal class encapsulate its class (external class) the members of can have access permissions to define common or Static internal Class Outer {Class Inner1 {} Class Inner2 {} Static Class Inner3 {}} in non-Static internal Class. only non-static methods can exist, static methods cannot exist. you can directly access external class attributes in non-static methods, including private and protected attributes. Package com. ibm. inner; public class Outer {private String name; public int I; protected floatf; // Inner is the internal class Inner {// you can directly access the attribute public void show () {name = "admin" of the external class in non-static methods of the internal class "; I = 10; f = 12.5f;} // In the static method of the internal class? Can I access external class attributes? // Public static void show2 () {// }}} define a static internal class in an external class. In a static internal class, there can be common methods or static methods. // static internal class, static class. inner2 {public void show3 () {} public static void show4 () {}} access the attributes of an external class in a static internal class? You cannot access non-static attributes in a static internal class. Otherwise, you can directly access static attributes in an external class in a static internal class. Static class Inner2 {public void show3 () {// name = "admin"; pass = "123";} public static void show4 () {pass = "456 ";}} if an external class contains a common method or a static method, how to call the method in the internal class. Call the external class method in the internal class. Non-static methods of the internal class can directly call non-static methods of the external class. In non-static methods of internal classes, you can directly call static methods of external classes. In static internal classes, neither static methods nor non-static methods can directly call non-static methods of external classes. Two ways to instantiate an internal class: 1. Create an object of a non-static internal class: the internal class object is created to call the internal class attributes or methods to create non-static internal class objects: External class. internal Class Object Name = new external class constructor. new internal class constructor; or external Class Object Name = new external class constructor; External class. internal class internal Class Object Name = new external Class Object Name. new internal class constructor; public staticvoid main (String [] args) {// create non-static internal Class Object Outer. inner oi = new Outer (). new Inner (); Outer outer = new Outer (); Outer. inner oi2 = outer. new Inner ();} If a static internal class object is created: External class. internal class internal Class Object Name = new external class name. internal class constructor; // create the object Outer of the static internal class. inner2 oi3 = new Outer. Inner2 (); the Inner class can also be defined inside the method. Local variables of the final type in the method can be accessed by methods of the Inner class. Public void show4 () {final String CW = "ADMIN"; intk = 12; // define a class in a method classInnerMethod {public void show5 () {System. out. println (CW); // System. out. println (k); // features of internal classes that cannot be accessed without final modification: 1 Innerclass can be declared as an abstract class, therefore, it can be inherited by other internal classes. It can also be declared as final. // Create an abstract internal class abstract class Inner3 {abstract void show6 ();} // create a common class to inherit Inner3 class Inner4extends Inner3 {@ Override void show6 () {}}// create a final class Inner5 {// The final class cannot be inherited} // class Inner6 extends Inner5 {//} 2 internal class can be declared private or protected, other classes cannot directly access the internal class. To access the internal class, you must access the internal class private class Inner6 {}protected class Inner7 {}3 Inner class through the external class, which can be declared as static, however, you cannot use the outer layer to encapsulate non-static member variables of the class. If an internal class is declared as static, In this case, this internal class can only access static attributes in the external class, but not static attributes. 4. Members of non-static internal classes cannot be declared as static, only the top-level class or static internal class can declare that static members cannot define static attributes or static methods in non-static internal classes. Summary: an internal class is a class defined in a class. If you want to talk about the information in a class, do not allow other classes to access directly, but only allow a class to access directly, then you can define this class as an internal class. Defining an internal class can directly call the attributes or methods of an external class. It is not recommended for internal classes.

Related Article

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.