Why can't an inner class object be created in main () in Java?

Source: Internet
Author: User

For the main method, although written in a class, it is free from any class, so the non-static inner class of a class is not directly visible to it and cannot be accessed directly.

1: Non-static inner class, you must have a reference to an external class to create.

2: In a non-static method of an external class, a non-static inner class can be created directly because there is an implied external class that references this.

3: In the static method of the outer class, because there is no this, you must first obtain the external class reference and then create a non-static inner class.

4: Static inner class that can be created directly without the need for an external class reference.

5: Static internal classes are also not directly accessible to non-static methods of external classes.

6: It can be speculated that the non-static inner class can directly access the method of the external class, because when creating a non-static inner class, there is an implicit external class reference is passed in.

 Public classOuterclass { Public Static voidMain (string[] args) {Outerclass Outerclass=NewOuterclass (); Innerclass Innerclass= Outerclass.NewInnerclass (); Staticinnerclass Staticinnerclass=NewStaticinnerclass (); }      Public voidfoo () {innerclass innerclass=NewInnerclass (); Staticinnerclass Staticinnerclass=NewStaticinnerclass (); }      Public classInnerclass {} Public Static classstaticinnerclass {}}

Why can't an inner class object be created in main () 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.