今天准备写一个串口通信的Java类,其中有个内部类,用来执行读写操作,但是在main方法中声明内部类的时候有错误提示:

No enclosing instance of type SPComm is accessible. Must qualify the allocation with an enclosing instance of type SPComm(e.g. x.new A() where x is an instance of SPComm) 

public static void main(String[] args){
	init();
	CommThread commThread = new CommThread();//错误提示
		
		
}
问题就出在CommThread这个内部类,它不是static的:

private class CommThread extends Thread{
		private final InputStream commInputStream;
		private final OutputStream commOutputStream;
		
		public CommThread(){
			commInputStream = inputStream;
			commOutputStream = outputStream;
		}
		
父类的static方法不能调用非static的成员,所以修改的方法是把CommThread类修改为static的:
private static class CommThread extends Thread...