Thread-related methods - public or private?

These questions have been driving me mental so here goes. For clarification, this program works perfectly, I’d just like to know if I can write it in a different, possibly better way.


public class Game extends Canvas implements Runnable {
	
	private static final long serialVersionUID = 1L;
	private static final int width = 400;
	private static final int height = 400;
	private BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	private boolean running = false;
	
	public static void main (String[] args) {
		
		Game game = new Game();
		game.setPreferredSize(new Dimension(width,height));
		game.setFocusable(true);
		game.addKeyListener(new Input());
		
		JFrame frame = new JFrame("Toast");
		frame.add(game);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		game.start();
		
	}
	
	
	public void start () {
		running = true;
		new Thread(this).start();
	}
	
	public void stop () {
		running = false;
	}
	
	public void run () {
		
		double before = System.currentTimeMillis();
		long now;
		double unprocessed = 0;
		
		while (running) {
			
			now = System.currentTimeMillis();
			unprocessed += (now-before)*60/1000;
			before = now;
			
			while (unprocessed >= 0) {
				update();
				unprocessed --;
			}
			
			render();
			
		}
		
	}
	
	public void update () {
		Player.update();
	}
	
	public void render () {
		BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}
		Graphics g = bs.getDrawGraphics();
		g.drawImage(image,0,0,getWidth(),getHeight(),null);
		g.setColor(Color.WHITE);
		g.fillRect(Math.round(Player.x),Math.round(Player.y),Player.w,Player.h);
		g.dispose();
		bs.show();
	}
	
}

The code has been shortened to make it more read-able.

Question 1: I can remove the method stop with no noticeable change. Is the method ever called and if not - why bother?

Question 2: Changing the method start to private doesn’t affect my program. However, I receive an error message when doing the same to method run. Why does run have to be public?

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Input implements KeyListener {
	
	static boolean left = false;
	static boolean right = false;
	static boolean up = false;
	static boolean down = false;
	
	public void keyPressed (KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_LEFT) {
			left = true;
		}
		if (keyCode == KeyEvent.VK_RIGHT) {
			right = true;
		}
		if (keyCode == KeyEvent.VK_UP) {
			up = true;
		}
		if (keyCode == KeyEvent.VK_DOWN) {
			down = true;
		}
	}
	
	public void keyReleased (KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_LEFT) {
			left = false;
		}
		if (keyCode == KeyEvent.VK_RIGHT) {
			right = false;
		}
		if (keyCode == KeyEvent.VK_UP) {
			up = false;
		}
		if (keyCode == KeyEvent.VK_DOWN) {
			down = false;
		}
	}
	
	public void keyTyped(KeyEvent e) {
		
	}
	
}

This is less important and less about this specific class but if you’re feeling generous.
Question 3: The static variables in my input-handling class are used, but never changed, in other classes. This brings to my total noob question. Can’t public variables be accessed by all classes? Why does a static type do the job for me?

I’d gladly appreciate some help!

It’s very clear that you didn’t write this code, or you simply copied it from another source.

  1. In the code given, stop is never called.

  2. start() is only used by the class that it’s defined in, so it can be private with no consequence.
    run() needs to be accessed by the thread class, therefore it needs public access.

  3. Public classes can be accessed by any class

“public static final” is a way of creating global constants that any class can access. Though, because they are private, making them static doesn’t do anything at all. At the same time, they are only used in this class, so that makes no difference.

Edit: Reading your 3rd question again, you mention your input handling class, which isn’t given, so my above answer probably doesn’t apply.

I’ve watched and read five or so tutorials and they have written in this exact way. I’m sure there are other ways but from my limited experience I’ve seen this countless times.

So there is no such thing as public variables? I still don’t understand which type to use. I added Input.java in my original post for reference.

Thanks for the help

Sorry, that was a typo. Yes, public fields are possible and can be accessed from any class.

static boolean left = false;
static boolean right = false;

Because these static variables have no access modifier (public, private, protected) they use the default access, which is package. So, any class in the same package can access these values. The only thing static does is allow them to be accessed without creating an object of the class, for example:

boolean isLeft = Input.left;

And for reference, the 4 access modifiers for fields and methods:

public - Any class can access them
private - only the class it is contained in can access it
protected - only subclasses can access it
package - Any class within the same package can access it

That cleared up things for me, thanks a lot!

public class SomeClass implements Runnable {

	@Override
	public void run() {
	}

}

run() can’t be anything else than public, because you inherit it from Runnable. In the above code, you see @Override, this indicates that it is inherited and you override it.
If you change public to private or protected, then save your project in Eclipse, it will show you an error and it will tell you why you can’t do that.