Java static internal class

Source: Internet
Author: User

I have been developing on the Microsoft. Net platform before. It cannot be instantiated in C #, and all its members are static. We generally use static classes as tool classes and directly use their attributes and methods without instantiating them,Static classes in the entire domain are unique..

When I started using Java, I found that Java static classes are generally used in the content of a class, referred to as static internal classes. In addition, this type can contain various non-static members like non-static classes, and can be instantiated at the same time. This directly subverts my perception of static classes.

Next I will compare static and non-static internal classes in Java:

public class outClass {    public static class innerClass{        public innerClass(){            System.out.println("innerClass");        }    }    public static void main(String[] args)     {         //outClass a = new outClass();        innerClass b = new innerClass();        innerClass c = new innerClass();            }    }
public class outClass {    public class innerClass{        public innerClass(){            System.out.println("innerClass");        }    }    public static void main(String[] args)     {         outClass a = new outClass();        innerClass b = a.new innerClass();        innerClass c = a.new innerClass();            }    }

Summary:

Through code, we found that non-static internal classes are affiliated to external class objects. An external class object needs to be instantiated first, and the non-static internal class can be instantiated through the external class object; static internal classes can be seen as directly affiliated external classes. This static means that the external classes are external classes rather than external class instances. External classes are unique in the process, static internal classes do not need to be unique. Multiple instances can be generated.

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.