苦恼的java问题~~

davidtps 2010-02-09 10:00:58
源码是:
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 错误


请问是哪出错了啊~~~~~~~~~~
...全文
137 9 打赏 收藏 转发到动态 举报
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
plplum 2010-02-09
  • 打赏
  • 举报
回复
可恶!!!!!!!!!!!!!
magicbu 2010-02-09
  • 打赏
  • 举报
回复
你现在的Runner1是内部类,把它变成外部类就好了
内部类隐式包含一个this引用指向包含它的外部类,但在main静态方法中,外部类TestThread还未实例化,当然this就找不到引用喽
bobo364 2010-02-09
  • 打赏
  • 举报
回复

/*
* 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里没有任何问题啊
final_xt 2010-02-09
  • 打赏
  • 举报
回复
原因是:静态方法无法引用内部类。。。
lvzhu18 2010-02-09
  • 打赏
  • 举报
回复
public class TestThread {
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);
}
}
}

}
allysniper 2010-02-09
  • 打赏
  • 举报
回复
直接将class Runner1 implements Runnable 改成static class Runner1 implements Runnable前面加个STATIC 即可
jypapgl 2010-02-09
  • 打赏
  • 举报
回复
楼上 很热心~ 我怎么是恶意灌水了呢
plplum 2010-02-09
  • 打赏
  • 举报
回复
楼主帮你改好了,看上面。
plplum 2010-02-09
  • 打赏
  • 举报
回复
public class TestThread {

public void test() {
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);
}
}
}

}
相关推荐

62,568

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告