0

I am trying to call from my NrlData class, but the following line keeps giving me an error

public static class NrlMain {
    public static void main(String[] args) {
        NrlData nrlData = new NrlData();//No enclosing instance of type mProgram is accessible. Must qualify the allocation with an enclosing instance of type mProgram (e.g. x.new A() where x is an instance of mProgram).

any help with how i can resolve that would be great

user2297518
  • 35
  • 1
  • 3
  • 8

2 Answers2

0

You need to either make your NrlData class static or create it from an instance of the enclosing class.

EG:

public static class NrlMain {
  static String aStaticVariable = "Static";
  String anInstanceVariable = "Hello";

  private class NrlData {
    // We CAN do this because there is always a parent `Object` of type `NrlMain`.
    String hello = anInstanceVariable;
    // We CAN also access static variables.
    String s = aStaticVariable;
  }

  private static class NrlStaticData {
    // We CANNOT do this because there is no parent object.
    //String hello = anInstanceVariable;
    // We CAN access static variables.
    String s = aStaticVariable;
  }

  public static void main(String[] args) {
    // Fails
    //NrlData nrlData = new NrlData();
    NrlData nrlData = new NrlMain().new NrlData();
    NrlStaticData nrlStaticData = new NrlStaticData();
  }

}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • can you please explain why? sorry i'm kind of new to java – user2297518 May 31 '13 at 13:21
  • @user2297518 - If the inner class is marked `static` it has no access to anything else other than `static` variables. If it is not marked static it is a real inner class and therefore can access variables from an object in its parent class, but, obviously, there must be an object of its parent class to access. – OldCurmudgeon May 31 '13 at 13:32
  • with your code you have NrlData as a private class in NrlMain. I have it as a seperate public class – user2297518 May 31 '13 at 13:43
  • You say above that `NrlData` is inside `mProgram` - well mine is equivalent, just inside `NrlMain` instead. If it was a separate public class you would not have this problem. – OldCurmudgeon May 31 '13 at 14:22
0

You have to create first an instance of mProgram before creating an instance of an inner class, or you can declare the inner class (NrlData in that case) as static, but you still need the mProgram class to access it (but you don't have to instantiate it.

public class mProgram {
    public class NrlData {
        ...
    }

    public static void main(String[] args) {
        mProgram.NrlData nrlData = new mProgram().new NrlData();
    }

    public void aMethod() { // accessing inner class from the "this" instance
        NrlData nrlData = new NrlData();
}

Or

public class mProgram {
    public static class NrlData {
        ...
    }

    public static void main(String[] args) {
        mProgram.NrlData nrlData = new mProgram.NrlData();
    }
}

Just take the first case, where NrlData is not static.

From within a non-static function inside mProgram class, you don't need to create a mProgram instance, because it uses the this instance.

Now if you try to access the inner class from another class, as you don't have any instance of mProgram, you'll have to create first that instance. It's the reason why you have the problem only in NrlMain and not in mProgram.

public class NrlMain {
    public void accessingInnerClass() {
        // Creating the mProgram instance
        mProgram mprogram = new mProgram();
        // Creating inner class instance
        mProgram.NrlData nrlData = mprogram.new NrlData();
    }
}
eternay
  • 3,754
  • 2
  • 29
  • 27