Java member Inner class

Source: Internet
Author: User

In reading Java some of the data structure source code, often find the use of member internal classes, such as node and TreeNode in HashMap, so we will summarize it.

1. member Inner class

(1) Internal and external access rights

Constructs an outer class and an inner class

 Public class Out {    private String str= "outstr";     Private void print ()    {        System.out.println (str);    }     Private class in{        public String instr= "InStr";           Public void Test ()        {            System.out.println (str);            Print ();     }}}

Test

 Public Static void Main (string[] args) {        out-out =new  out ();        Out.inin =out. New In ();        In.test ();    }

The result shows that the inner class can arbitrarily access the member function member variables of the outer class, and the outer class cannot freely access the members of the inner class.

In addition, the inner class can have modifiers, such as private, but it succeeded.

(2) will the inner class be initialized when the external class is initialized?

 Public class Out {    public  out ()    {        System.out.println ("Out");    }      Public class in{        public in  ()        {            System.out.println ("in");        }    }         publicstaticvoid  main (string[] args) {out-out        =New  out ();    }}

Well, obviously not.

(3) How to deal with internal and external method name conflicts

 Public classOut { Public voidTest () {System.out.println ("Out"); }     Public classin{ Public voidTest () {System.out.println ("In"); }    }     Public Static voidMain (string[] args) {out-out=Newout (); Out.in in=out.NewIn ();    In.test (); }}

Apparently calling the inner class itself, the method of the outer class is hidden away

2. Static member Inner class

(1) Internal and external access rights

 Public classOut {PrivateString str= "Outstr"; Private StaticString static_str= "Static_str";  Public Static classin{ Public voidTest () {System.out.println (STATIC_STR); }    }         Public Static voidMain (string[] args) {//out out=new out ();In in=NewIn ();    In.test (); }}

The result is that the inner class can now invoke only the static members of the outer class, and the initialization of the inner class is no longer entirely dependent on the outer class.

(2) will the inner class be initialized when the external class is initialized?

 Public class Out {    public  out ()    {        System.out.println ("Out");    }      Public Static class in{        public in  ()        {            System.out.println ("in");        }    }         publicstaticvoid  main (string[] args) {out-out        =New  out ();        }}

Or not?

Java member Inner 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.