Java (24)-Inner class details

Source: Internet
Author: User

I. Internal class:


1). Definition of the inner class: the popular saying is to define a class within a class.

2). The inner class can be public,private,protected and so on, or it can be statically static.


Two. Four types of internal classes are implemented:


1). Static Inner class:

Example:

Class Innerclass {public static int count = 10;//static inner class public static class Inner{public void print () {System.out.println (cou NT);}}} public class Staticinnerclass {public static void main (string[] args) {innerclass.inner  Inner = new Innerclass.inner ( ); Inner.print ();}}

Results: 10

Static inner classes can only access static member variables and static methods of external classes. Because the inner class is statically initialized, you can directly new the object of the inner class.


2). member Inner class:

Example:

Class Memberclass{public int count = 10;public static int number = 1;//member intrinsic class public class inner{public void print () {                   Sy Stem.out.println (count+ "   " +number);}} public class Memberclasstest {public static void main (string[] args) {///through an object of an outer class, new out of the inner class object. Memberclass.inner mem = (new Memberclass ()). New Inner (); Mem.print ();}}

Results: 10 1 member inner class as a member of an external class, you can use all members and methods of an external class directly, creating an inner class object to create an object of an outer class before creating an inner class object: Outerclass.innerclass inner = (new Outerclass ())  . New Innerclass (); If you want to use member variables of an external class in a member's inner class, you need: outerclass.this. Variable name.

3). Local Inner class:

Example:

Class localinnerclass{public void Test () {class inner{public void print () {System.out.println ("HAHA");}} New Inner (). print ();}} public class Localinnerclasstest {public static void main (string[] args) {localinnerclass  inn = new Localinnerclass () ; Inn.test ();}}

Results: HAHA

A local inner class is specified in a method or scope. Can only be used in this method or scope and cannot be used after exiting these scopes.

Local inner classes can only access variables of the final type in the method.


4). Anonymous Inner class:

Example:

Class anonymousclass{@SuppressWarnings ("deprecation") public void print (Date  data) {        System.out.println ( Data.tolocalestring ());}} public class Anonymousclasstest {public static void main (string[] args) {Anonymousclass a = new Anonymousclass (); an.print (new date ()); An.print (new Date () {@Override @deprecatedpublic String tolocalestring () {//TODO auto-generated method Stubreturn "HAHA";}});}}

Results:

2015-3-17 21:28:11
HAHA

An anonymous inner class implicitly inherits a parent class or an interface. Anonymous inner classes are not accessible modifiers. This inner class is used more on events.






Java (24)-Inner class details

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.