1
package shubh;
class thread5
{
    static class a7 extends Thread
    {
        public void run()
        {
            for(int i=0;i<=10;i++)
            {
                System.out.println(i);
            }
        }
    }

    static class a8 implements Runnable
    {
        public void run()
        {
            for(int i=21;i<=30;i++)
            {
                System.out.println(i);
            }
        }
    }

    public static void main(String arg[])
    {
        a7 a=new a7();
        a.start();
        a8 b=new a8();
        Thread th=new Thread(b);
        th.start();
        for(int i=40;i<=50;i++)
        {
            System.out.println(i);
        }
    }
}

when i remove static keyword from the 2 nested classes it is giving error non static variable cannot be referenced from a static context can anyone explain me the meaning of this error or why it is necessary to give nested class as static

1

4 Answers 4

6

If you remove static then you need an actual object in order to instantiate the inner classes. I.e.
new thread5().new a7();

Sign up to request clarification or add additional context in comments.

2 Comments

If the class is static then you can access it as it is part of the class and not part of an instance.If you declare it as non-static i.e. remove static then you can still use it, but you must access it via an instance as it is no longer part of the class
What are you not clear about?static means it belongs to class and not instance.non-static means it is part of an instance.I.e. you must have an instance (in this case new Thread5()) to access it.
1

A non-static inner class can only be instantiated within an instance of the class, for example:

thread5 t = new thread5();
a7 a = t.new a7();

Comments

0

when i remove static keyword from the 2 nested classes it is giving error non static variable cannot be referenced from a static context

You can't directly access non-static data/methods/class's from a static method, you need an instance of a class to access them.

If you remove static for class a7, you should create a7 instance like this.

Threads5 td = new Threads5()';
a7 a=new td.new a7();

Comments

0

Found an answer in This Javarevisited Article

Now before finding answer of compiler error "non-static variable cannot be referenced from a static context", let's have a quick revision of static. Static variable in Java belongs to Class and its value remains same for all instance. static variable initialized when class is loaded into JVM on the other hand instance variable has different value for each instances and they get created when instance of an object is created either by using new() operator or using reflection like Class.newInstance(). So if you try to access a non static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until an instance is created and they are associated with any instance. So in my opinion only reason which make sense to disallow non static or instance variable inside static context is non existence of instance.

In summary since code in static context can be run even without creating any instance of class, it does not make sense asking value for an specific instance which is not yet created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.