非静态变量,不能从静态上下文引用

时间:2015-03-27 18:48:53

标签: java

我的行sb.init(this);总是带回“非静态变量,这不能从静态上下文中引用”。我无法弄清楚原因。

package asteroidgame;

import blobmx.BlobGUI;
import blobmx.SandBox;
import blobmx.SandBoxMode;
import java.util.Random;

public class AsteroidGame implements BlobGUI{

private static final Random random = new Random();

public static void main(String[] args) {
    AsteroidGame();
}    
public static void AsteroidGame(){ 
    SandBox sb = new SandBox();
    sb.setSandBoxMode(SandBoxMode.FLOW);
    sb.setFrameRate(66);
    sb.init(this);
}

3 个答案:

答案 0 :(得分:1)

this不能在静态方法中使用。由于静态方法中没有实例表示。

答案 1 :(得分:0)

变化

public static void main(String[] args) {
    AsteroidGame();
}   

public static void main(String[] args) {
    AsteroidGame myAsteroidGame = new AsteroidGame();
}  

然后对myAsteroidGame做任何你想做的事情(比如调用方法或任何你想要的)

答案 2 :(得分:0)

这是修复:

package asteroidgame;

import blobmx.BlobGUI;
import blobmx.SandBox;
import blobmx.SandBoxMode;
import java.util.Random;


public class AsteroidGame implements BlobGUI{

private static final Random random = new Random();
public static final SandBox sb = new SandBox();

public static void main(String[] args) {
    AsteroidGame newgame = new AsteroidGame();
}    
public AsteroidGame(){ 
    //SandBox sb = new SandBox();
    sb.setSandBoxMode(SandBoxMode.FLOW);
    sb.setFrameRate(66);
    sb.init(this);
}
相关问题