3-4 embedded class: local class

Source: Internet
Author: User

Local class-local class

1. Remove the "static" keyword in the declaration of a static member class and move its definition to the static method or static initialization of its external class.CodeSegment becomes a local static member class

2. A member class is a local member class by moving its definition to the instance method or instance initialization code of its external class.

Public class Outer {private int instancefield; Private Static int staticfield; // 1/4-define a local member class in the "instance initialization code segment" {int localvirable1 = 0; final int localvirable2 = 1; Class inner1 {public inner1 () {// you can directly access the instancefield of the external class = 1; outer. this. instancefield = 1; // system. out. print (localvirable1); // local variables cannot access system. out. print (localvirable2); // local constants can be accessed} // local classes cannot have permissions, because local class/* Public class inner2 {} */} is not used externally at all. // 2/4-define a local static member class static {class inner2 {public inner2 () {staticfield = 1; // The static domain that can access the external class // instancefield = 2; // The non-static domain cannot be accessed, because it has not been created} public void intancemethod () {// 3/4-define the local class inner3 {}// outer In the instance method. inner2 inner2; // The local class is valid only in its block} Private Static void staticmethod () {// 4/4-define the local class inner4 {public inner4 () {staticfield = 2 ;}} in the static method ;}} // The interface is not a local class/* interface I {}*/}}

-- Features
A local class can only access local variables declared as final in the code segment to which it belongs. This is because the code segment is returned after execution.
The local variable disappears, but the instance of the local class can still exist.

-- Constraint
1. Because the internal class is only visible in the code segment that defines it, there is no permission to set it.
2. Because of its small visibility, the interface is defined as meaningless.
3. duplicate names with external classes are not allowed

-- When to use
All are used in the form of anonymous classes

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.