-3

My line sb.init(this); always comes back with a "non static variable this cannot be referenced from a static context." I cant figure out why.

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 Answers3

1

this cannot be used inside static method. As there is no instance representation inside the static method.

Crazyjavahacking
  • 8,777
  • 2
  • 30
  • 40
  • pbabcdefp was right about it being a constructor. I removed the static void part and it fixed the first problem but now my call to the constructor says it cannot find the symbol: AsteroidGame() – Christopher Mar 27 '15 at 19:00
0

change

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

to

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

then do whatever you want to do with myAsteroidGame (such as calling the method or whatever you want)

adrCoder
  • 2,975
  • 2
  • 25
  • 49
0

This was the fix:

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);
}
  • This was my answer 20 minutes ago. Please accept my answer http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – adrCoder Mar 27 '15 at 19:21