1

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?

Community
  • 1
  • 1
Chaitanya
  • 3,399
  • 6
  • 29
  • 47
  • @EJP I do agree this is a duplicate, but this actually points correctly to problem. In that example if you see the entire code of nearly 100lines is given. Pointing out errors would be difficult in that. Thats why I have posted this again. :) – Chaitanya Jan 30 '13 at 12:57

5 Answers5

6

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.
Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

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();
    }
}
Rishi Kolvekar
  • 649
  • 3
  • 7
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();
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

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.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
0

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

Hello.fun();

should work!

NightWhisper
  • 510
  • 1
  • 4
  • 12