Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create an object of class which implements Runnable interface

I am trying to create an object of a class which implements Runnable interface in the main method and passing those objects to the thread pools. But the IDE shows error which reads non-static variable this cannot be referenced from a static context i.e., I couldn't create the objects in the first place. I couldn't figure out what is exactly wrong with this code. Everything else works fine, but just this line of code is showing compile error. Can anyone help??

package threads;

import java.util.concurrent.*;

public class Tut5 {

    public static void main(String[] args) {

        ExecutorService exe = Executors.newFixedThreadPool(2);

        for(int i=0; i<5; i++) {
            Runner5 r5 = new Runner5(i);
            exe.submit(r5);
        }

        exe.shutdown();

        System.out.println("All tasks submitted.");

        try {
            exe.awaitTermination(1, TimeUnit.DAYS);
        }
        catch(InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All tasks completed.");
    }

    class Runner5 implements Runnable {

        private int id;

        public Runner5(int id) {
            this.id = id;
        }

        public void run() {

            System.out.println("Starting thread: " + id);

            try{
                Thread.sleep(3000);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("Ending thread: " + id);
        }
    }
}

There u go @jtahlborn

like image 214
h-rai Avatar asked Dec 11 '25 12:12

h-rai


2 Answers

  • non-static variable this cannot be referenced from a static context -

this error comes when you are using not static instance or method in static method or block.

And main method is static public static void main(String[] args) so its simple it will populate error.

Here your class Runner5 implements Runnable class is inner class which you are accessing in static main method so it generate -

No enclosing instance of type Tut5 is accessible.
Must qualify the allocation with an enclosing instance of type Tut5
(e.g. x.new A() where x is an instance of Tut5).

you can define this inner class out of your Tut5 class or mark this Runner5 as static. or create an instance of Tut5 class and create a method and place your rest of the code.

Thanks

like image 91
Subhrajyoti Majumder Avatar answered Dec 14 '25 02:12

Subhrajyoti Majumder


As others have answered, the Runner5 class is an inner class of Tut5. As such, it needs an outer instance to be instantiated.

It you make Runner5 a static class, it becomes a static nested class which does not need an outer instance.

static class Runner5 implements Runnable {
...
}

But, as others have noted, you should put the class in its own class file, unless you are working on an exercise on nested classes, or you are sure the class is only useful in combination with the class Tut5, and will only be referenced from that class.

Seee http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for some insight.

like image 43
Christian Semrau Avatar answered Dec 14 '25 02:12

Christian Semrau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!