源码是:
public class TestThread {
public static void main(String [] args) {
Runner1 r1 = new Runner1();
Thread td = new Thread(r1);
td.start(); for (int j = 1; j < 100 ;j++ ){
System.out.println("Thread :" + j);
}
} class Runner1 implements Runnable{
public void run () {
for (int i = 0; i<100 ;i++ ){
System.out.println("runner1:" + i);
}
}
}
}
错误是:D:\Test>javac TestThread.java
TestThread.java:4: 无法从静态上下文中引用非静态 变量 this
Runner1 r1 = new Runner1();
^
1 错误
请问是哪出错了啊~~~~~~~~~~
public class TestThread {
public static void main(String [] args) {
Runner1 r1 = new Runner1();
Thread td = new Thread(r1);
td.start(); for (int j = 1; j < 100 ;j++ ){
System.out.println("Thread :" + j);
}
} class Runner1 implements Runnable{
public void run () {
for (int i = 0; i<100 ;i++ ){
System.out.println("runner1:" + i);
}
}
}
}
错误是:D:\Test>javac TestThread.java
TestThread.java:4: 无法从静态上下文中引用非静态 变量 this
Runner1 r1 = new Runner1();
^
1 错误
请问是哪出错了啊~~~~~~~~~~
Runner1 r1 = new Runner1();
Thread td = new Thread(r1);
td.start(); for (int j = 1; j < 100; j++) {
System.out.println("Thread :" + j);
}
} public static void main(String[] args) {
TestThread testThread = new TestThread();
testThread.test(); } class Runner1 implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("runner1:" + i);
}
}
}}
public static void main(String[] args) {
Runner1 r1 = new TestThread().new Runner1();
Thread td = new Thread(r1);
td.start(); for (int j = 1; j < 100; j++) {
System.out.println("Thread :" + j);
} } class Runner1 implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("runner1:" + i);
}
}
}}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package exp1;/**
*
* @author szhu5
*/
public class TestThread
{
public static void main(String [] args)
{
Runner1 r1 = new Runner1();
Thread td = new Thread(r1);
td.start(); for (int j = 1; j < 100 ;j++ )
{
System.out.println("Thread :" + j);
}
}
}/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package exp1;/**
*
* @author szhu5
*/
public class Runner1 implements Runnable
{
public void run ()
{
for (int i = 0; i <100 ;i++ )
{
System.out.println("runner1:" + i);
}
}
} /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package exp1;/**
*
* @author szhu5
*/
public class Runner1 implements Runnable
{
public void run ()
{
for (int i = 0; i <100 ;i++ )
{
System.out.println("runner1:" + i);
}
}
}
netbean里没有任何问题啊
内部类隐式包含一个this引用指向包含它的外部类,但在main静态方法中,外部类TestThread还未实例化,当然this就找不到引用喽