Why non-static variable cannot be reference from a

2019-05-30 00:40发布

问题:

Possible Duplicate:
non-static variable cannot be referenced from a static context (java)

public class DemoJava {

    public class Hello {

        void fun()
        {
            System.out.println("This is static fun man!!");    
        }
    }

    public static void main(String[] args) {

        Hello hello = new Hello();
        hello.fun();
    }
}

In this example it will give me an error since I am trying to access a non-static class from a static method. Fine. For instance, if I have the same Hello class in another file and I do the same thing it does not give me an error.

Even in that case we are trying to access non-static class from static method. But that doesn't give any error. Why?

回答1:

In this example it will give me an error since I am trying to access a non-static class from a static method.

No, it will give you an error because you're trying to create an instance of an inner class (which implicitly has a reference to an instance of the enclosing class) when you don't have an instance of the enclosing class.

The problem isn't the call to fun() - it's the constructor call.

For example, you can fix this by using:

DemoJava demo = new DemoJava();
Hello hello = demo.new Hello();

Or you could just make it a nested but not inner class, by changing the class declaration to:

public static class Hello

Read section 8.1.3 of the JLS for more information on inner classes, and section 15.9.2 for determining enclosing instances for a class instance creation expression:

Otherwise, C is an inner member class (§8.5), and then:

  • If the class instance creation expression is an unqualified class instance creation expression, then:

    • If the class instance creation expression occurs in a static context, then a compile-time error occurs.


回答2:

yes, it will give you error, correct way of doing it is

public class DemoJava {

    public class Hello {

        void fun()
        {
            System.out.println("This is static fun man!!");    
        }
    }

    public static void main(String[] args) {

        DemoJava demoJava = new DemoJava();
        Hello  hello = demoJava.new Hello(); //you need to access your inner class through instance object
        hello.fun();
    }
}


回答3:

you have to create an instance of Outer class in-order to create the instance of your inner class.

From Documentation:

To instantiate an inner class, you must first instantiate the outer class.

syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

DemoJava demoInst=new DemoJava();
Hello hello = demoInst.new Hello();
        hello.fun();


回答4:

Make class Hello static

public static class Hello {

    void fun()
    {
        System.out.println("This is static fun man!!");    
    }
}

Your inner class Hello dous not need access to an instance of the outer class DemoJava therefore it can be made static.



回答5:

You can always call the static functions of a class without having an instance

Hello.fun();

should work!



标签: java class