The difference between object-oriented static inner classes and non-static inner classes in the first season of getting Started with Java

Source: Internet
Author: User

Thank you for your advice in the Inner class section, "Happy Yang". Use static inner classes. Here is a brief talk about the use of static internal classes.

Package Com.devin;public class mymain{private static String name = "Woobo";     Private String num = "X001"; Static inner classes can be decorated with public,protected,private static class Person {///static inner class can be defined statically or non-static members private String Addres        s = "China";        private static String x = "as"; Public String mail = "http://blog.csdn.net/qq_32059827";//internal class publicly-owned member (common) void display () {//System.out . println (num);//non-static members with no direct access to external classes//static inner classes cannot access non-static members of external classes (including non-static variables and non-static methods) System.out.println (name);//Only Straight        Access to static members of external classes//static inner classes can only access static members of external classes (including static variables and static methods) System.out.println ("Inner" + address);//Access this inner class member.        }} public void Printinfo () {Person person = new person ();        An external class accesses a non-static member of an inner class: instantiates an inner class to Person.display (); System.out.println (mail)///unreachable//SYSTEM.OUT.PRINTLN (address)//unreachable System.out.println (person.address); You can access the private member of the Inner class System.out.println (person.x);//The external class accesses the static members of the Inner class: internalclass. Static member System.out.println (person.mail);//access to the public member of the Inner class} publicly static void main (string[] args) {Myma        In statictest = new Mymain ();    Statictest.printinfo (); }}

1. Objects of nested classes do not require objects of their enclosing classes. That is, it can be instantiated without relying on an external class instance.

2. Non-static perimeter class objects cannot be accessed from objects in the nested class. This is defined by the Java syntax in which "static methods cannot access non-static members directly"

3. The external class accesses the members of the inner class in particular and cannot be accessed directly, but can be accessed through an internal class instance because all members and methods within the static nesting default to static. Also note that the inner static class person is only visible within the class Statictest scope, It is wrong to refer to or initialize in other classes.
4. Static inner classes can have static members, and non-static inner classes cannot have static members.
5. A non-static member of a static inner class can access the static variables of the outer class, and cannot access the non-static variables of the outer class;

6. Non-static members of non-static inner classes can access non-static variables of the outer class.

Generating a static inner class does not require an external class member: This is the difference between a static inner class and a member inner class. Objects of a static inner class can be generated directly: Outer.Inner in = new Outer.Inner (), rather than generated by generating an external class object. This actually makes the static inner Class A top class (under normal circumstances, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static.) Just place the nested class inside the interface's namespace, which does not violate the rules of the interface)



The difference between object-oriented static inner classes and non-static inner classes in the first season of getting Started with Java

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.