"Go" Java inner class types and usage parsing

Source: Internet
Author: User
Tags class definition getdate

Java internal class types and usage parsingInner class inner class

The related classes are organized together, reducing the clutter in the namespaces.

An inner class can be defined in another class and can be defined in a function or even as part of an expression.

  the inner classes in Java are divided into four types :

  Static inner class static inner class (also called nested Class)

  member Inner class member inner class

  Local internal class local inner class

  Anonymous inner class anonymous inner class

static inner class static Inner class

The simplest form of inner class.

The static keyword is added to the class definition .

Cannot have the same name as an external class.

is compiled into a completely separate. class file with the name Outerclass$innerclass.class in the form.

  only static members and static methods of external classes can be accessed , including private static members and methods.

The static inner class object is generated in the following way:

  Outerclass.innerclass inner = new Outerclass.innerclass ();

Static internal classes use code:

PackageCom.learnjava.innerclass;Classstaticinner{PrivateStaticint a = 4;//Static Inner classPublicstatic class Inner { Span style= "color: #0000ff;" >public void Test () {// // System.out.println (a);}}} public classpublic static void main (string[] args) {Staticinner.inner Inner = new  Staticinner.inner (); Inner.test ();}}        

member Inner class member Inner class

The member inner class is also defined in another class, but is not defined with a static adornment.

A member inner class and a static inner class can be likened to non-static member variables and static member variables.

The member inner class is like an instance variable.

  It can access all the member variables and methods of its outer class, whether static or non-static .

Create an instance of the member's inner class inside the outer class:

  This.new Innerclass ();

Create an instance of an inner class outside the outer class:

  (New Outerclass ()). New Innerclass ();

To access members of an external class in an inner class:

  Outerclass.this.member

See the code example for details:

PackageCom.learnjava.innerclass;Classmemberinner{Privateint d = 1;Privateint a = 2;//Define a member inner classPublicClassInner2 {Privateint a = 8;PublicvoidDoSomething () {//Direct access to external class objectsSystem.out.println (d); System.out.println (a);// If you access a directly, you are accessing a // This.a); }}}public classpublic static void Main (string[] args) {// // You need to create an instance of the outer class first memberinner.inner2 inner = new Memberinner ().  Inner2 (); inner.dosomething ();}}        

local internal class local Inner class

Local inner classes are defined in a method and are smaller than the range of methods. Is the least used type in an inner class.

Like local variables, it cannot be modified by public, protected, private, and static.

Only local variables of the final type defined in the method can be accessed.

Local inner classes are defined in methods, so they can only be used in methods, that is, you can only generate instances of local inner classes within a method and invoke their methods.

PackageCom.learnjava.innerclass;Classlocalinner{int a = 1;PublicvoidDoSomething () {int B = 2;Finalint c = 3;//Define a local inner classClassInner3 {PublicvoidTest () {System.out.println ("Hello World"); System.out.println (a);//Non-final local variables cannot be accessed//Error:cannot refer to a non-final variable B inside an inner//class defined in a different method//System.out.println (b);// System.out.println (c);}} // Create an instance of the local inner class and call the method new  Inner3 (). Test ();}} public classpublic static void Main (string[] args) {//  Create an external class object Localinner inner = new  Localinner (); // call methods of the outer class  

anonymous inner class anonymous Inner class

The anonymous inner class is a local inner class without a name, not using the keyword class, extends, implements, and no constructor method.

An anonymous inner class implicitly inherits a parent class or implements an interface    .

Anonymous inner classes are used more often as a method parameter.

PackageCom.learnjava.innerclass;ImportJava.util.Date;PublicClassanonymouseinnerclass{@SuppressWarnings ("deprecation")PublicString getDate (date date) {ReturnDate.tolocalestring (); }PublicStaticvoidMain (string[] args) {Anonymouseinnerclass test =NewAnonymouseinnerclass ();//Date Printed: String str = test.getdate (NewDate ()); System.out.println (str); System.out.println ("----------------");//Use the anonymous inner class String str2 = Test.getdate (NewDate () {});// using curly braces, but not filling in the contents, the execution result is exactly the same as above // An object that inherits the subclass of the date class // Use the anonymous inner class, and override the method in the parent class String STR3 = Test.getdate (new Date () {// Overrides a method in the parent class  @Override @Deprecated public String tolocalestring () {return "Hello:" + super.tolocalestring (); } }); System.out.println (STR3); }} 

In the generated. class file, the anonymous class generates the Outerclass$1.class file, which is based on the first few anonymous classes.

Examples of using internal classes in swing are as follows:

PackageCom.learnjava.innerclass;ImportJava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;ImportJava.awt.event.WindowEvent;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;PublicClassswingtest{PublicStaticvoidMain (string[] args) {JFrame frame =New JFrame ("JFrame"); JButton button =New JButton ("JButton"); Button.addactionlistener (NewActionListener () {//New comes out an instance of a class that implements the ActionListener interface@OverridePublicvoidactionperformed (ActionEvent arg0) {System.out.println ("Hello World"); } });// join button  Frame.getcontentpane (). Add (button); // Frame.setdefaultcloseoperation ( Jframe.exit_on_close); Frame.setsize (200new  Windowadapter () {// You can also use an anonymous inner class that inherits the adapter class  @Override public void windowclosing (WindowEvent e) {System.out.println ("Closing" ); System.exit (0true     

"Go" Java inner class types and usage parsing

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.