How to get access to variables from other class

Viewed 34

So I would like to use the variable "kierunek" from this class

public class Snake extends JPanel implements ActionListener{
char kierunek = 'D';

to this class

import pl.java.Snake.*;

public class MyKeyListener extends KeyAdapter {

importing shows "Unused import statement", I tried to find soulution, but there is nothing helping me

2 Answers

"Unused import statement" Means that you haven't used your imported class yet.

It triggers for any class, like for example, good old scanner

import java.util.Scanner;

You will get the same warning until you use said class it. In this case, by creating a Scanner object:

Scanner input = new Scanner(System.in);

Also, do you have both of your classes inside the same package? If so, you don't have to import it, you can just call it.

(Like Daniel said, I recommend using setters and getters when interacting with your class attributes so you can keep them private)

Related