Java Anonymous object, inner class, modifier, code block

Source: Internet
Author: User
Tags class definition modifier

An anonymous object is an object that is being built only when the object's statement method is created and the object's address is not assigned to a variable, the anonymous object can only be called once, and a new anonymous object needs to be created when it wants to be called again

Create a Normal object: person P =new person ();

Create an Anonymous object: New Person (). Method name ();

New Person (). SetName (" Wang no ");

Inner class: Written in the inner or member or local location of other classes, which is called external classes by other classes. You can access the properties of an external class in an inner class.

When an inner class is written in a member position, it is called a member inner class, which is written in a local location, called a local inner class.

member inner class definition format: As basic as defining a common class

Class external Name {

public class internal class name () {}

}

Access mode: External class name internal class name Variable name =new external class name (). New inner class name ()

 Public class a {  //external class     b{//      internal class         public void  C () {            System.out.println ("This is a member class");     }}}
new A (). New b ();        AA.C ();

How local internal variables are defined:

Class external Name {

Modifier return value Method name () {

Class Inner classes {other code}

}

}

Access: Create an inner class object externally and invoke the method in the Main method

 package   com.oracle.demo04;  public  class  a {//  External class  public  void   C () { class  d{//  inner class  public  void   E () {System.out.println (" This is an inner class ");             }}  new   D (). E (); }}
A aa =new  A ();        AA.C ();

Modifier

Public

Protected

Default

Private

In the same class

In the same package (subclass and unrelated classes)

Sub-classes of different packages

Unrelated classes in different packages

L to be able to access only in this class using the private decoration;

L to think that the classes in this package can be accessed without modifiers;

L to think of classes in this package and subclasses in other packages can be accessed using the protected adornment

I want all the classes in the package to be accessible using the public adornment.

Note: If the class is decorated with public, the class name must be the same as the file name. There can be only one public-decorated class in a file.

code block

A local code block is defined in a method or statement

  

Constructing a code block is a block of code that defines the position of a member in a class

Characteristics:

Takes precedence over construction method execution, and constructs code blocks to perform initialization actions that are required for all objects

Each object that is created executes a block of construction code at a time.

A static code block is a block of code that is defined at the member location, using the static adornment .

Characteristics:

It takes precedence over the execution of the Main method, which takes precedence over the construction of code block execution, when used in any form for the first time to the class.

The static code block executes only once, regardless of how many objects are created.

Can be used to assign a value to a static variable for initialization of a class.

1  Packagecom.oracle.demo04;2 3  Public classStatic {4     PrivateString name;5     Private intAge ;6     /*constructs a block of code to create an object to execute once*/7     //Building Code Blocks8     {9System.out.println ("This is the construction code block");Ten     } One     /*A static block of code that executes once when the class first creates the object, regardless of the number of times it is created , A * Does not execute, executes once, takes precedence over other blocks of code*/ -     //Static code block -     Static{ theSystem.out.println ("This is a static code block"); -     } -     //Local code block -      Public voidShow () { +System.out.println ("This is a partial code block"); -     } +}

Java Anonymous object, inner class, modifier, code block

Related Article

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.