No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

Source: Internet
Author: User
Tags static class

 PackageCom.thread; Public classTHREAD01 { Public classThread1extendsthread{PrivateString name;  PublicThread1 (String name) { This. Name =name; }          Public voidrun () { for(inti = 0; I < 5; i++) {System.out.println (name+ "Run:" +i); Try{sleep (int) Math.random () *10); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }            }         Public Static voidMain (string[] args) { Thread1 thread02 = new Thread1 ("A"); Thread1 thread02=NewThread1 ("B"); }}

When writing this code, the compiler is Thread1 thread02 = new Thread1 ("A") andwill always report no enclosing instance of type E is accessible. Must qualify the allocation with an enclosing error, I looked up the data on the internet found that: in Java, static methods in a class cannot call dynamic methods 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.

We can change the public class Thread1 to public static class Thread1

No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

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.