Java reading note 15 (inner class in Java)

Source: Internet
Author: User
Tags modifiers

Preface

Java from the time of JDK1.1, began to introduce the concept of inner class, then the small part of the blog to share some of the characteristics of the internal classes in Java.


what is an internal class?

In many cases, a class is defined as a separate program unit, but sometimes a class is placed within another class's internal definition, and the class defined inside the other class is called 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-point difference in addition to being defined within other classes.

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

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

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


non-static inner class

Defining inner classes is also very simple, as long as you put one class inside another class, you can even place it inside the method.

package com.test;public Class Cow{private Double weight;//the two overloaded constructors of the outer class public Cow () {}public Cow (double weight) {this.weight = weight;} Two instance variables that define a non-static inner class private class cowleg{//non-static inner class private double length;private String color;//Two overloaded constructors public for non-static inner classes Cowleg () {}public Cowleg (double length, String color) {this.length = Length;this.color = color;} The following omits length, color setter and getter method 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;} An instance method of a non-static inner class public void info () {System.out.println ("The current leg color is:" + color + ", High:" + length ");//member variable System that accesses the private decoration of an external class directly   . OUT.PRINTLN ("The cow's leg is heavy:" + 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 ();}}  

Members of the non-static inner class above can access the private members of the outer class, but this is not the reverse. Members of non-static inner classes are known only within the scope of non-static inner classes and cannot be used directly by external classes. If an external class requires access to a member of a non-static inner class, you must explicitly create a non-static inner class object to invoke access to its instance members.

Package Com.test;public class Outer{private int outprop = 9;class inner{private int inprop = 5;public void Acessouterprop ( {//non-static inner class can directly access the external class's Private member Variable SYSTEM.OUT.PRINTLN ("Outprop Value of the outer class:" + Outprop);}} public void Accessinnerprop () {///external class cannot directly access instance variables of non-static inner classes,//The following code has a compile error//SYSTEM.OUT.PRINTLN ("Inprop Value of the inner class:" + Inprop);// To access an instance variable of an inner class, you must explicitly create an inner class object System.out.println ("Inprop value for the inner class:" + New Inner (). Inprop);} public static void Main (string[] args) {//executes the following code, creates only the Outer class object, and has not created an inner class object Outer out = new Outer ();      ①out.accessinnerprop ();}}


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

static inner class

If static is used to decorate an inner class, the inner class belongs to the outer class itself and not to an object of the outer class, so an inner class using the static adornment is called the class inner class, and in some places it is called a static inner class. A static inner class can contain static members, or it can contain non-static members. Static internal classes cannot access instance members of external classes, and can access only class members of external classes, depending on the rules for which static members cannot access non-static members.


Package Com.test;public class Staticinnerclasstest{private int prop1 = 5;private static int prop2 = 9;static class Statici nnerclass{//static inner class can contain static member private static int age;public void Accessouterprop () {//code below error:// The static inner class cannot access an instance variable of the external class System.out.println (PROP1);//The following code is normal System.out.println (PROP2);}}


Anonymous Inner class

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

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

2. Anonymous inner classes cannot define constructors, because anonymous inner classes do not have class names, so constructors cannot be defined

Package Com.test;interface Product{public Double getprice ();p ublic String getName ();} public class Anonymoustest{public void Test (Product p) {System.out.println ("purchased a" + p.getname () + ", took out" + p.getprice ());} public static void Main (string[] args) {anonymoustest ta = new Anonymoustest ();//Call the test () method, you need to pass in a product parameter,// An instance of its anonymous implementation class is passed here Ta.test (new Product () {public double GetPrice () {return 567.8;} Public String GetName () {return "AGP graphics card";}});}}


Java reading note 15 (inner class in Java)

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.