Within Java class

Source: Internet
Author: User
Tags control characters

Inner classes are divided into: local inner classes: Local inner classes are classes defined in the methods of a class. Note: Because the local inner class can only be accessed in the method in which it resides, the local inner classaccess control permissions and the static modifier cannot be used。
Cases:
public class Localinnerclass{public static void Main (string[] args) {class test{                     void Print () {                         System.out.println ("Hello local inner Class");                     } } new Test (). print ();//Create an object of the local inner class and call the instance method print. }}
member inner class--Non-static inner class:                 :   ,         nbsp           1.   A non-static inner class can be used as an instance member of an external class, so it can use arbitrary access control characters such as private, protected, public, and so on.                         2. Method of non-static inner class when accessing a variable, the system takes precedence over the method to find out whether the name exists within the A local variable that is used if it exists, or, if it does not exist, in the inner class where the method resides                         &NB Sp   Find out if a member variable exists for that name.                         3. Non-static inner classes cannot have static members. Cases:
public class cow{private double weight;      Public Cow () {} public Cow (double weight) {this.weight = weight;  }//create non-static inner class private cowleg{private double length;//Pants length private String color;  The color of the trousers public Cowleg () {//no parameter construction} public Cowleg (double length, String color) {//parameter constructed this.length = length;  This.color = color;  The public void info () {//is used to display System.out.print ("The current jeans color is:" +color+ "Long:" +length); }} public void Test () {Cowleg cl = new Cowleg ("Blue");//Create an instance of the Inner class Cl.info ();//Call the Member method System.out.println ("width:" +  weight); public static void Main (string[] args) {new Cow. Test ();//Create Cow Instance call member method}} 
member inner class--Static inner class: NOTE: 1. Static inner classes can be used as class members of external classes.               2. Static inner Classes cannot access instance members of external classes, only class members of external classes. 3. A static inner class can have a class member or instance member. Cases:
public class staticinnerclasstest{          //create Static inner class static class staticinnerclass{private static int age;//define ages public Staticinnerclass () {  //no parameter constructs}public staticinnerclass (int age) {                       this.age = age;} public void Accessouterprop () {  //used to display the member variable                       System.out.println (age);}} public static void Main (string[] args) {new staticinnerclasstest.staticinnerclass. Accessouterprop ();// Creates an instance of a static inner class and calls its member method Accessouterprop. }}

Anonymous inner class: the anonymous inner class is primarily used for classes that need to be created only once. Note: Before Java8, Java requires that local variables accessed by local inner classes, anonymous inner classes must use the final decoration, and that the restriction is canceled from Java8, Java8 more intelligent: If the local variable is accessed by an anonymous inner class, the local variable is equivalent to automatically using the FINA     L Decoration. Cases:
Interface product{public void GetPrice ();} public class Anonymoustest{public static void Main (string[] args) {Product PD = new Product () {public                     void GetPrice () { C1/>system.out.println ("100 Yuan");}                     ;PD. GetPrice ();}}




Within Java class

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.