I have problem with declaration of variable. iam trying to prepare code using Multi threading
but i have problem with declaration of variable. Now iam confused, whether is possible to put Scanner into second not main class- i suppose that yes, but i don´t know how declare variable. Exception is – Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – non-static variable this cannot be referenced from a static context
at test1.PIN.main …
public class PIN{
static int a;
class Runner extends Thread{
public void run(){
Scanner sc = new Scanner(System.in);
for(int i= 1; i<4; i++){
System.out.println("PUT your PIN: ");
int a = sc.nextInt();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(PIN.class.getName()).log(Level.SEVERE, null, ex);
}
if(a ==1234){
System.out.println("PIN OK");
} else {System.out.println("PIN NOK");}
}
}
}
public static void main(String[] args){
Runner r = new Runner();
r.start();
Declare
Runnerstatic inner class. Non static inner classes are instance bound so you need outer class instance to create object of non-static inner class. And because your inner class is not static you can not access it insidemainwhich isstatic-context