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
CodePudding user response:
With Java you will have to either have to create a public Getter Method, or make the variable itself public.
As a object oriented Language Java differentiates between different access modifiers. More on that here: What is the difference between public, protected, package-private and private in Java?
Usually one should use Getter and Setter as accessors for the variables in order to avoid creating Problems.
More on why here: Why use getters and setters/accessors?
CodePudding user response:
"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)