Understanding java-8.9 Nested classes from the beginning

Source: Internet
Author: User

In this chapter we discuss nested classes.

1. Concept

Inner classes are nested classes when static is used.

Package Com.ray.ch07;public class Test {private static Class mytest3{}}

MYTEST3 is a nested class


2. Attention points

(1) can only communicate with the static part of the outer class

(2) Do not hold references to external class objects, you can use the static property directly

Package Com.ray.ch07;public class Test {private int id = 1;private static String name = "AAA";p rivate Static class MyTest1 {public void Say () {//System.out.println (ID);//errorsystem.out.println (name);}} Private class MyTest2 {public void Say () {System.out.println (ID); SYSTEM.OUT.PRINTLN (name);}} public static void Main (string[] args) {//New test (). New MyTest1 ();//error.new test.mytest1 (). Say (); new test (). New Mytes T2 ();}}

From the above code can be seen, MyTest1 is not able to access the ID, and MYTEST2 can.

Also, at new, we can see that the MyTest1 has the static characteristics, does not need new, but still need to add a parenthesis, and MyTest2 need new to invoke the method inside.


(3) Nested classes do not have this reference

Because nested classes use static, you cannot use this to refer to external classes.


Summary: This chapter discusses the concept of nested classes and the points of attention.


This chapter is here, thank you.

-----------------------------------

Directory



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding java-8.9 Nested classes from the beginning

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.