Inner classes in Java

Source: Internet
Author: User

An inner class (Inner Class) is a class that is defined in another class. Corresponding to this, the class containing the inner class is called the outer class. The related classes are organized together, reducing the clutter in the namespaces.

Benefits of the Inner class:

1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class

2. The method of the inner class can access all the data of the external class directly, including the private data

3. Functions implemented by internal classes can also be implemented using external classes, but sometimes it is easier to use internal classes

The inner classes can be divided into the following types:

    • member Inner class member inner class
    • Static inner class static inner class (also called nested Class)
    • Method Inner class local inner class
    • Anonymous inner class anonymous inner class
Member Inner classes in Java

The most common inner class is the member inner class, also known as the ordinary inner class. Let's look at the following code:

The result of the operation is:

As we can see from the code above, the use of the members ' inner classes is as follows:

1. The Inner class is defined inside the Outer class and is equivalent to the position of a member variable of the Outer class, and the Inner class can use any access control, such as public, protected, private, etc.

2. The test () method defined in the Inner class can directly access the data in the Outer class without being affected by the access control, such as direct access to private property A in the Outer class

3, after defining the member inner class, you must use the Outer class object to create the inner class object, but not directly to the new inner class object, namely: The Inner class object name = Outer class object. New inner class ();

4, after compiling the above program, you will find that two. class files have been generated

Where the second is the. class file for the outer class, the first is the. class file for the inner class, that is, the. class file for the member's inner class is always the case: the outer class name $ internal class name. class

In addition, friendly hints OH:

1. External classes cannot use the members and methods of the inner class directly.

You can first create an object of an inner class and then access its member variables and methods through the objects of the inner class.

2. If the external class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class, you can use the This keyword. Such as:

Operation Result:

Static inner classes in Java

Static inner classes are internal classes of static adornments, which are characterized by:

1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external Class (). Members

2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name

3. When creating an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner Class ();

Operation Result:

Method inner classes in Java

The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.

Note: because the method inner class cannot be used outside the methods of the outer class, the method inner class cannot use the access control and the static modifier.

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.

Anonymous internal class Usage test

Package com.learnjava.innerclass;

Import Java.util.Date;

public class Anonymouseinnerclass

{

@SuppressWarnings ("deprecation")

Public String getDate (date date)

{

return date.tolocalestring ();

}

public static void Main (string[] args)

{

Anonymouseinnerclass test = new Anonymouseinnerclass ();

Print Date:

String str = test.getdate (new Date ());

System.out.println (str);

System.out.println ("----------------");

Using anonymous inner classes

String str2 = test.getdate (New Date ()

{

});//use curly braces, but do not fill in the content, the execution results and the above is exactly the same

An object that inherits a subclass of the date class was generated

System.out.println (STR2);

System.out.println ("----------------");

Use anonymous inner class, and override methods in parent class

String STR3 = test.getdate (New Date ()

{

Overriding methods in a 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.

Inner classes in Java

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.