Simple analysis of nested classes and inner classes in Java

Source: Internet
Author: User
Tags final variables static class

Before looking at the <java programming thought >, I saw the difference between nested classes and internal classes, but later forgot their concepts. Yesterday in looking at < data structure and algorithm analysis (Java language Edition) >, encountered this concept, then a big doubt: what is the difference between nested classes and internal classes? Is it the only difference between a keyword static?

So today I found a time to check the details of the difference between the two, summed up in this blog, not only to facilitate their own review and learning, but also to enlighten others.

1, Concept:

A class that is defined within a class, called a "nested class". Nested classes are divided into two types: static and not static. The latter has a special name called "Inner class". So from the concept, it can be seen that the nested class is the owning relationship with the inner class, and the latter is contained in the former. The sample code is as follows:

Class Outerclass {
    ...
    Static class Staticnestedclass {
        ...
    }
    Class Innerclass {
        ...}}

Also, a nested class is a member of the class in which it resides. An inner class can access all members of the class in which it is located, even if the member is private. Static nested classes are not allowed to access members of the class in which they reside. At the same time, nested classes, static and non static, can be declared private, public, protected, and default.

2, why should I use nested classes?

The benefits should be more textual, later in the use of the process to understand and experience it: the use of only one place in the logical grouping of classes, increased encapsulation, easy to read and maintain.

3,static Nested classes:

Because static nested classes cannot directly access the static member variables and methods of the class in which they reside, a static nested class must be accessed by binding an instance of the class in which it resides. Static members and methods for the class in which they reside include private, protected, and public, which can be accessed. Because it also has a static modifier.

A static nested class instantiates and accesses its internal members by writing out the encapsulated class name:

Outerclass.staticnestedclass nestedobject =
     new Outerclass.staticnestedclass ();

4, internal class:

Because an inner class is a member of the class in which it is located, it can access any variable and method of its class, but it itself cannot define any static variables or methods.

Also, an inner class is instantiated differently from a static nested class:

Outerclass outerobject=new Outerclass ();
Outerclass.innerclass innerobject = Outerobject.new innerclass ();

5, the classification of the internal classes:

Previously contacted the classification of the internal classes, here summed up:

In the past, some of the so-called interview books inside are almost all of the internal class into four categories:

Static inner classes (both static nested classes), member inner classes (both internal classes above), local inner classes, and anonymous inner classes. The first two have been introduced, the following is a special look at the latter two.

5.1, local internal classes:

A class defined within a method is called a "local inner class". Its scope is limited to the scope of the method and can only be defined and instantiated within the scope of the method, and is the least useful class type. As with local variables, it cannot be decorated as private, public, protected, and static, and can only access final variables defined within the method.

Class Localinner
{
    int a = 1;
    
    public void dosomething ()
    {
        int b = 2;
        Final int c = 3;
        Defines a local inner class
        Inner3
        {public
            void Test ()
            {
                System.out.println ("Hello World");
                System.out.println (a);
    
                Non-final local variable
                //Error:cannot refer to a non-final variable B inside a inner
                //class defined in a Diffe Rent method
                //System.out.println (b);
    
                Can access final variable
                System.out.println (c);
            }
        }
    
        Creates an instance of the local inner class and invokes the method
        new Inner3 (). Test ();
    
public class Localinnerclasstest
{public
    static void Main (string[] args)
    {
        //Create external class object
        Localinner inner = new Localinner ();
        Calling the external class method
        inner.dosomething ();
    }
    

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.