can't create the instance of the second class
Hi again! Can't get why the Eclipse underlines the line below
msg error: No enclosing instance of type HelloRunnable is accessible. Must qualify the allocation with an enclosing instance of type HelloRunnable (e.g. x.new A() where x is an instance of HelloRunnable).
Code :
public class HelloRunnable implements Runnable {
//run method for HelloRunnable i.e. for the thread #1
public void run(){
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
}
public class HelloRunnable2 implements Runnable {
// run method for HelloRunnable2 i.e. for the thread #2
public void run() {
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
(new Thread(new HelloRunnable())).start();
(new Thread([COLOR="Red"]new HelloRunnable2()[/COLOR])).start();
}
}
Re: can't create the instance of the second class
HelloWorld2 class needs to be static.
Also, I THINK declaring a public class more than once goes against convention? I can't be sure :P
i.e. use class or private class instead for inner classes.
Re: can't create the instance of the second class
Hi to everyone, actually i wanna know why we have to use Threads and in this case what the guy above has posted what is purpose of that code since we can output that he wants to output just with writting this code in main class:
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
---------------------------------------------------------------------------------------------
public class HelloRunnable implements Runnable {
//run method for HelloRunnable i.e. for the thread #1
public void run(){
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
}
public class HelloRunnable2 implements Runnable {
// run method for HelloRunnable2 i.e. for the thread #2
public void run() {
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
(new Thread(new HelloRunnable())).start();
(new Thread(new HelloRunnable2())).start();
}
}
what is actually the difference doing this with thread and doing without threads since the result is the same?
Anyone who can explain it ?