shubh
shubh

Reputation: 21

static class in java

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

Upvotes: 1

Views: 147

Answers (4)

Bohemian
Bohemian

Reputation: 425033

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();

Upvotes: 1

Foggzie
Foggzie

Reputation: 9821

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.

Upvotes: 0

PermGenError
PermGenError

Reputation: 46408

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();

Upvotes: 0

Cratylus
Cratylus

Reputation: 54074

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

Upvotes: 6

Related Questions