Reputation: 93
I'm trying to create an array of objects as defined by a subclass (I think that's the correct terminology). I can see that the question is recurring, but implementation is still problematic.
My code
public class Test {
private class MyClass {
int bar = -1;
}
private static MyClass[] foo;
public static void main(String args[]) {
foo = new MyClass[1];
foo[0].bar = 0;
}
}
Gives the error
Exception in thread "main" java.lang.NullPointerException.
In an attempt to rationalise it, I broke it down to simplest terms:
public class Test {
private static int[] foo;
public static void main(String args[]) {
foo = new int[1];
foo[0] = 0;
}
}
Which appears to work. I just don't see the difference between my two examples. (I understand that my first is pointless, but MyClass will ultimately contain more data.)
I'm pretty sure the question is asked here and is very well answered. I think I implemented the solution:
MyClass[] foo = new MyClass[10];
foo[0] = new MyClass();
foo[0].bar = 0;
but the second line of the above issues the error
No enclosing instance of type Test is accessible.
I do understand that ArrayList would be a way forward, but I'm trying to grasp the underlying concepts.
NB - It might be useful to know that while very comfortable with programming in general, Java is my first dip into Object Oriented programming.
Upvotes: 5
Views: 59922
Reputation: 55589
The reason int
works, but MyClass
doesn't:
From here:
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false
When you initialize an array, all elements take on the default value.
So, when you initialize an int[]
, all elements are 0, so no problem using that or assigning a new value to it.
But, when you initialize an MyClass[]
, all elements are null
, which is a problem when you try to access a member of one of the elements.
If you don't know why accessing a null
object's members won't work, you probably need to take 2 steps back and read a Java book.
Additional Note:
Technically, this:
int[] foo = new int[1];
foo[0] = 0;
is actually more like this:
MyClass[] foo = new MyClass[10];
foo[0] = new MyClass();
not:
MyClass[] foo = new MyClass[10];
foo[0].bar = 0;
since you're assigning a new value to an element, rather than accessing a member of an element.
No enclosing instance of type Test is accessible:
The other answers cover that pretty well, and here are 3 related questions:
No enclosing instance of type is accessible.
No enclosing instance of type Server is accessible
"No enclosing instance of type" error while calling method from another class in Android
Upvotes: 3
Reputation: 41347
You cannot use a non-static inner class from within the static main
method.
The solution is to declare MyClass
as private static class
.
Upvotes: 0
Reputation: 5082
The class
MyClass
is an inner class
, and not a subclass. Non-static inner classes can be accessed by creating an object of class enclosing the inner class. So, if you want to access the inner class, you would have to create an object of outer class first. You can do it by:
Test t = new Test();
MyClass[] foo = new MyClass[10];
foo[0] = t.new MyClass();
foo.bar = 0;
Upvotes: 6
Reputation: 308011
The problem you faced at the end ("no enclosing instance") is not actually related to arrays at all.
Try replacing your last code block with this:
MyClass foo = new MyClass();
You'll get the exact same error message, even though no arrays are involved.
The problem here is that a non-static inner class has an implicit reference to its outer instance. Since you have no outer instance (you're in a static context, there is no this
), no MyClass
instance can be created.
You probably didn't need/want a inner class and can simply make it static
:
private static class MyClass
Also: the reason your code worked with int
and not with MyClass
is that an int[]
holds int
values (int
is a primitive type), while a MyClass[]
holds MyClass
references (MyClass
is a reference type).
Upvotes: 3