Reputation: 30136
The following are user requirements which I cannot change:
A class with a static interface (all methods are static).
One specific method returns an ArrayList<String>
object and a File
object.
The following is my implementation of the above requirements:
An inner class which contains an ArrayList<String>
object and a File
object.
The specific method initializes an instance of the inner class and returns it at the end.
But I am unable to create an instance of the inner class without creating an instance of the outer class:
public class Outer
{
public class Inner
{
ArrayList<String> strings;
File file;
}
public static Inner method()
{
Inner inner = new Outer().new Inner();
...
return inner;
}
}
Besides being "ugly", the new Outer().new Inner()
feels pretty incorrect.
Is there a different approach that I can take in order to avoid having to instantiate the outer class?
Upvotes: 3
Views: 1519
Reputation: 79838
If you make the inner class static (that is, you write public static class Inner
) then you don't need an instance of the outer class; you can just write new Outer.Inner()
.
Upvotes: 6