I am trying to call from my NrlData class, but the following line keeps giving me an error
我正在尝试调用我的NrlData类,但是下面的代码总是给我一个错误。
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
任何帮助我如何解决那将是伟大的。
2 个解决方案
#1
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.
在创建一个内部类的实例之前,您必须先创建一个mProgram的实例,或者您可以将内部类声明为静态类,但您仍然需要mProgram类来访问它(但您不必实例化它)。
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.
就拿第一个例子来说,NrlData不是静态的。
From within a non-static function inside mProgram class, you don't need to create a mProgram instance, because it uses the this
instance.
在mProgram类内部的非静态函数中,您不需要创建一个mProgram实例,因为它使用了这个实例。
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.
现在,如果您试图从另一个类访问内部类,因为您没有任何mProgram实例,您必须首先创建该实例。这就是为什么你只在NrlMain而不是在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();
}
}
#2
0
You need to either make your NrlData
class static
or create it from an instance of the enclosing class.
您需要使NrlData类保持静态,或者从封闭类的实例中创建它。
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();
}
}
#1
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.
在创建一个内部类的实例之前,您必须先创建一个mProgram的实例,或者您可以将内部类声明为静态类,但您仍然需要mProgram类来访问它(但您不必实例化它)。
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.
就拿第一个例子来说,NrlData不是静态的。
From within a non-static function inside mProgram class, you don't need to create a mProgram instance, because it uses the this
instance.
在mProgram类内部的非静态函数中,您不需要创建一个mProgram实例,因为它使用了这个实例。
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.
现在,如果您试图从另一个类访问内部类,因为您没有任何mProgram实例,您必须首先创建该实例。这就是为什么你只在NrlMain而不是在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();
}
}
#2
0
You need to either make your NrlData
class static
or create it from an instance of the enclosing class.
您需要使NrlData类保持静态,或者从封闭类的实例中创建它。
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();
}
}