Study notes--java inner class exercises

Source: Internet
Author: User

1. Try writing an anonymous inner class in the method.

Package Com.lzw;public class Anonymityinnerclass {}class outerclass4{public outinterface doit (final String s) {return new Outinterface () {private int i=0;public int getValue () {System.out.println (s); return i;} public void F () {System.out.println ("F ()");}};}} Interface Outinterface {//define an interface}

Anonymous inner class: New Outinterface () {

.......

} is written in the method of the external class Outerclass4.

2. Try writing the Main method in a static inner class.

 PackageCOM.LZW; Public classStaticinnerclass {intx=100; Static classinner{voidDoitinner () {//System.out.println ("external class" +x);        }         Public Static voidMain (String args[]) {System.out.println (A); }    }}

Note: The most characteristic of a static inner class is that non-static members of the outer class cannot be used.

3. Try to write a static inner class that creates an instance of its inner class in the Main method.

Package Test;public class Staticinnerclass {static Class a{void F () {System.out.println ("F ()");}} public static void Main (String args[]) {STATICINNERCLASS.A a=new a (); A.f ();}}

  

Study notes--java inner class exercises

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.