Java appears No enclosing instance of type Test1 is accessible. (Turn, oneself also met!) )

Source: Internet
Author: User
Tags static class

Recently in Java, when compiling an example of writing a book, because the code is only part of the book, so he complements an internal class. The result appears at compile time: No enclosing instance of type E is accessible.  Must qualify the allocation with an enclosing instance of type E (e.g. X.new A () where x is an instance of E). E refers to the inner class that I wrote.

As a hint, there is no instance of an accessible inner class E that must be assigned an instance of an appropriate inner class E (such as X.new A () and x must be an instance of E. look at this hint, I wonder, I have used new instantiation of this class, why not yet.

So Baidu Google a bit of relevant information. Originally I wrote the inner class is dynamic , that is, the beginning of the public class start. and the main program is public static class main. In Java, a static method in a class cannot invoke a dynamic method directly . Only an inner class is decorated as a static class, and then the member variables and member methods of the class can be called in a static class. So in the case of no other changes , the simplest solution is to change the public class to public static class.

Another solution: To write these classes separately, that is, to put them in different files, but also to solve the problem

Summarize:

A static method in a class cannot call a dynamic method directly.

1  2 3  Packageindex01;4 5  6 7  Public classTest1 {8 9  Ten  One   A  -  Public Static voidMain (string[] args) { -  the   -  -System.out.println ("1"); -  +person P1 =NewPerson ("Andy", "Beijing", "UIC"); -  + System.out.println (P1.info ()); A  at   -  -   -  -System.out.println ("2"); -  inTeacher T1 =NewTeacher ("Andy", "Zhuhai", "UIC", "student"); -  to System.out.println (T1.info ()); +  -     } the  *   $ Panax Notoginseng   Public Static classPerson { -  the PrivateString name; +  A PrivateString School; the  + PrivateString location; -  $   $  - Person (string name, string school) { -  the  This. name=name; - Wuyi  This. school=School; the  - } Wu  - Person (string name, string location, string school) { About  $  This. name=name; -  -  This. location=Location ; -  A  This. school=School; +  the } -  $  PublicString info () { the  the return"Name:" + name+ "\nlocation:" + location + "\nschool:" +School; the  the } -  in } the  the   Public Static classTeacherextendsperson{ About  the PrivateString position; the  the   +  -   the Bayi Teacher (string name, string school) { the  the Super(name, school); -  -   the  the } the  the Teacher (string name, string location, string school) { -  the Super(name, location, school); the  the  94  the } the  the Teacher (string name, string location, string school, string position) {98  About Super(name, location, school); - 101  This. position=position;102 103 } 104  the  PublicString info () {106 107 return Super. info () + "\nposition:" +position;108 109 } the 111   } the 113  } the  the  
testcal

Java appears No enclosing instance of type Test1 is accessible. (Turn, oneself also met!) )

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.