1
Aprender a programar desde cero / Non-static variable cannot be referenced from a static context Java error cómo
« en: 01 de Mayo 2020, 04:28 »
Hola, tengo este error que me aparece <<Non-static variable cannot be referenced from a static context java>> en este código. Ya intenté varias cosas pero ninguna sirvió, ¿me podrian ayudar?
Código: [Seleccionar]
public class EjemploHilos {
public static void main(String[] args) {
HiloA a=new HiloA();
HiloB b=new HiloB();
HiloC c=new HiloC();
Thread hilo1=new Thread(a);
Thread hilo2=new Thread(b);
Thread hilo3=new Thread(c);
hilo1.start();
hilo2.start();
hilo3.start();
}
public class HiloA implements Runnable{
public HiloA() {}
public void run(){
System.out.println("Se ejecuta Hilo A");
System.out.println("sigo 1 en Hilo A");
System.out.println("informacion del hilo 1;\n"+Thread.currentThread());
try{
Thread.sleep(400);
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("sigo 2 en Hilo A");
}
}
public class HiloB implements Runnable{
public HiloB() {}
public void run(){
System.out.println("Se ejecuta Hilo B");
try{
Thread.sleep(400);
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("sigo 1 en Hilo B");
System.out.println("informacion del hilo 2;\n"+Thread.currentThread());
System.out.println("sigo 2 en Hilo B");
}
}
public class HiloC implements Runnable{
public HiloC() {}
public void run(){
System.out.println("Se ejecuta Hilo C");
System.out.println("sigo 1 en Hilo C");
System.out.println("informacion del hilo 3;\n"+Thread.currentThread());
try{
Thread.sleep(400);
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("sigo 2 en Hilo ");
}
}
}