Organize Java reading notes 15 of the internal class _java in Java

Source: Internet
Author: User
Tags class definition instance method modifiers static class

An inner class is the definition of a class within an external class. The class name does not need to be the same as the folder.
* Internal classes can be static, or public,default,protected and private adornments. (The external top-level class, which is the same as the class name and file name, can only use public and default.)

Objective

Java from the time of JDK1.1, began to introduce the concept of internal classes, then the small series also through this blog to share some of the characteristics of the internal classes in Java.

What is an inner class?

In many cases, a class is defined as a separate program unit, but sometimes it also puts a class within the internal definition of another class, which is defined within the other class as an inner class.

Syntactically, the syntax for defining an inner class and defining an outer class is roughly the same, and the inner class has the following two differences, in addition to being defined within other classes.

1. Internal analogy the outer class uses three modifiers: private--protected, static--outer classes cannot use these three modifiers.

2. Non-static internal classes cannot have static members.

In addition to the above two points, the other places are the same as the ordinary class, the following list of common internal class operations.

Non-static inner class

Defining an inner class is also very simple, as long as you place a class within another class definition, or even within the method.

<span style= "Font-family:comic Sans ms;font-size:18px;" 
>package com.test; 
  public class Cow {private double weight; 
  The two overloaded constructors public Cow () {} of an external class () {} public Cow (double weight) {this.weight = weight; 
    //Define a Non-static internal class private class Cowleg {//Non-static inner class Two instance variables private double length; 
    private String color; Two overloaded constructors public Cowleg () {} of Non-static inner class () {} public Cowleg (double length, String color) {this.length = Len 
      Gth 
    This.color = color; 
    //below omit length, color setter and Getter methods public void SetLength (double length) {this.length = length; 
    Public double GetLength () {return this.length; 
    public void SetColor (String color) {this.color = color; 
    Public String GetColor () {return this.color; The instance method of the//non-static inner class public void info () {System.out.println ("Current beef leg color is:" + color + ", High:" + Leng 
     TH); Direct access to the external class of the private-decorated member variable SYSTEM.OUT.PRINTLN ("The cow leg where the cow weight:" + weight); 
    ①}} public void Test () {Cowleg cl = new Cowleg (1.12, "Black and White"); 
  Cl.info (); 
    public static void Main (string[] args) {Cow Cow = new Cow (378.9); 
  Cow.test ();  }} </span>

Members of the Non-static internal class above can access private members of the external class, but the reverse is not true. A member of a non-static inner class is knowable only within a non-static inner class scope and cannot be used directly by external classes. If an external class needs to access members of a non-static inner class, you must explicitly create a non-static inner class object to invoke access to its instance members.

<span style= "Font-family:comic Sans ms;font-size:18px;" >package com.test; 
public class Outer 
{ 
  private int outprop = 9; 
  Class Inner 
  { 
    private int inprop = 5; 
    The public void Acessouterprop () 
    { 
      //non-static inner class can directly access the private member variable System.out.println of the outer class 
      (the Outprop value of the outer class:) 
        + Outprop); 
    } 
  The public void Accessinnerprop () 
  { 
    //external class cannot directly access an instance variable of a non-static inner class,//The 
    following code has a compile error 
    //System.out.println (" Inprop value of inner class: "+ Inprop"; 
    To access an instance variable of an inner class, you must explicitly create an internal class object 
    System.out.println (the Inprop value of the inner class: " 
      + new Inner (). Inprop); 
  } 
  public static void Main (string[] args) 
  { 
    //executes the following code, creates only the external class object, and does not create an internal class object 
    Outer out = new Outer ();   ① 
    Out.accessinnerprop (); 
  } 
 

Note: Static methods, static member variables, and static initialization blocks cannot be in non-static inner classes.

Static Inner class

If you use static to decorate an inner class, the inner class belongs to the outer class itself and not to an object of the outer class, so the inner class that uses static decoration is called the class inner class, and some places are also called static inner classes. Static inner classes can contain static members or non-static members. Static inner classes cannot access instance members of external classes and can only access class members of external classes, depending on the rule that static members cannot access non-static members.

<span style= "Font-family:comic Sans ms;font-size:18px;" >package com.test; 
public class Staticinnerclasstest 
{ 
  private int prop1 = 5; 
  private static int prop2 = 9; 
  The static class Staticinnerclass 
  { 
    //static inner class can contain statically member 
    private static int age; 
    public void Accessouterprop () 
    { 
      ///The following code 
      has an error://Static inner class cannot access an instance variable 
      System.out.println (PROP1) of an external class; 
      The following code is normal 
      System.out.println (PROP2);}}} 
 

Anonymous inner class

An anonymous inner class allows you to create a class that only needs to be used once, and creates an instance of that class immediately when you create an anonymous inner class, but you also need to be aware of the rules

1. An anonymous inner class cannot be an abstract class because the system creates an object of an anonymous inner class immediately when it creates an anonymous inner class, so it is not allowed to define an anonymous inner class as an abstract class

2. An anonymous inner class cannot define a constructor, because the anonymous inner class has no class name, so the constructor cannot be defined

<span style= "Font-family:comic Sans ms;font-size:18px;" >package com.test; 
Interface Product 
{public 
  double getprice (); 
  Public String getName (); 
} 
public class Anonymoustest 
{public 
  void Test (Product p) 
  { 
    System.out.println ("purchased one" + p.getname () C11/>+ ", flowers Off" + p.getprice ()); 
  } 
  public static void Main (string[] args) 
  { 
    anonymoustest ta = new Anonymoustest (); 
    When you call the test () method, you need to pass in a Product parameter, 
    //Where an instance of its anonymous implementation class 
    Ta.test (New Product () 
    {public 
      double getprice) is passed in. () 
      {return 
        567.8; 
      } 
      Public String getName () 
      {return 
        "AGP video card";}} 
    ) 
  ; 
} </span>

Here is a summary of the following internal categories of common editing

The

(1), inner class is still a separate class, and after compilation The inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class.
(2), internal classes cannot be accessed in a normal way. An inner class is a member of an external class, so an inner class can freely access the member variables of the external class, whether private or not.
(3), inner class declared static, can not casually access the external class member variable, at this time the inner class can only access the static member variables of the external class.

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.