-1

I created a nested static class like this:

public class OuterClass {

  public static class NestedClass {
    public static String getName() {
      //Some stuff
      return "Name";
    }
  }

  //Now am not able to call the method *getName()* inside *OuterClass*
  NestedClass.getName(); //Compile complains here
}

But I can do it from another class

public class TestOuterClass {
  public void testName() {
    OuterClass.NestedClass.getName();
  }
}

I don't understand why it doesn't work withen the class its been defined.

noMAD
  • 7,744
  • 19
  • 56
  • 94

1 Answers1

2

Place NestedClass.getName(); inside a method such as main.

This compiles and runs properly:

public class OuterClass {

  public static class NestedClass {
    public static String getName() {
      //Some stuff
      return "Name";
    }
  }

  public static void main(String[] args)
  {
      System.out.println(NestedClass.getName());
  }
}

Output:

$ javac OuterClass.java
$ java OuterClass
Name
$
rgettman
  • 176,041
  • 30
  • 275
  • 357