Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error on tokens, delete these tokens [closed]

Tags:

java

Here is my code that has been giving the problems.

package ca.rhinoza.game;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public boolean running = false;


public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 160;
    public static final int HEIGHT = WIDTH / 12 * 9;
    public static final int SCALE = 3;
    public static final String NAME = "Game";

    private JFrame frame;

    public Game(){
        setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
        setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
        setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));

        frame = new JFrame(NAME);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        frame.add(this, BorderLayout.CENTER);
        frame.pack();

        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public synchronized void start(){

        new Thread(this).start();

    }

    public synchronized void stop(){

    }

    public void run() {


    }


    public static void main(String[] args){

        new Game().start();
    }


}

I have no idea why it is doing this.

Edit: I edited as per your request to see more code.

like image 395
Rijnhardt Avatar asked Jul 08 '13 22:07

Rijnhardt


2 Answers

You put

public boolean running = false;

outside of a class definition, effectively in the global namespace. But there are no globals in Java. This is not legal.

I'll say, that's a strange error message for that though. I would expect a little more from the compiler. Something to the effect of class or interface or enum because once you start the statement with public there are exactly three legal things that can follow. But, it is what it is. So, the compiler is right to complain, I just would have expected a more informative error message.

like image 81
jason Avatar answered Nov 17 '22 19:11

jason


You are declaring a field outside of the class:

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public boolean running = false; /// <=============== invalid location 


public class Game extends Canvas implements Runnable { // <==== class starts here
    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 160;
    public static final int HEIGHT = WIDTH / 12 * 9;
    public static final int SCALE = 3;
    public static final String NAME = "Game";
    ...

It must be placed inside a class:

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;


public class Game extends Canvas implements Runnable { // <==== class starts here

    public boolean running = false; /// <=============== valid location

    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 160;
    public static final int HEIGHT = WIDTH / 12 * 9;
    public static final int SCALE = 3;
    public static final String NAME = "Game";
    ...
like image 33
acdcjunior Avatar answered Nov 17 '22 18:11

acdcjunior