'Access nested class list from outer class
I have a class that has a static nested class with two lists. And one of the functions within the outer class has the return type of this nested class. And this class takes in two lists in the constructor.
public class A {
public static class Data {
List<? extends Model> createdObjects;
List<? extends Model> removedObjects;
public Data(List<? extends Model> createdObjects , List<? extends Model> removedObjects) {
this.createdObjects = createdObjects;
this.removedObjects = removedObjects;
}
public List<? extends Model> getCreatedObjects() {
return this.createdObjects;
}
public List<? extends Model> getRemovedObjects() {
return this.removedObjects;
}
}
public Data getData() {
List<Model> modelList = createObject();
modelList.add(model);
// Question - How should I be returning the data in the createdObjects?
//Is this the correct way to create the data object and return it?
A.Data data = new A.Data(modelList, Collection.EMPTY_LIST);
return data;
}
}
}
So from I what know, createdObjects (list) can take a list of Model objects or List of an object that extends Model. So in my case, the createObject() creates a list, let's say List class, that extends from Model1 which in turn extends from Model. So I suppose I can create a list of MyModel, and then call the Data constructor along with two other empty lists (for the sake of discussion). Could anyone confirm if this would be a correct way of doing it?
Solution 1:[1]
You asked
Question - How can I access the data class and assign the created objects to the create list of nested class?
The answer of this is "depends"
You excluded access modifiers on the class A
and the Data
constructor. That means that they are only visible from the same package. Assuming this is true, they can only be accessed by a class that resides in the same package.
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// can be accessed this way
Data myData = new Data(new ArrayList<Model>(), new ArrayList<Model>());
// or this other way
Data myOtherData = A.getData();
}
}
So, if you want these classes to be accessible outside the package they reside in, you MUST make them public.
public class A {
public static class Data {
List<? extends Model> create;
List<? extends Model> remove;
public Data(List<? extends Model> create, List<? extends Model> remove) {
this.create = create;
this.remove = remove;
}
}
// Since Data is a static field, this method is not needed.
public static Data getData() {
// TODO method body and return statement here
}
}
Also, I suggest you control modification of Data
through the A
class. For example, the list objects should be pass by A
to Data
and not through the Data
constructor. IF YOU MUST pass the lists through the Data
constructor for some reason, then maybe you do need the getData()
function.
Solution 2:[2]
If I understand your question properly, this is what I am suggesting, all code inside of your Class A
:
public static Data getData(){
// 2 list parameters for passing inside `Data` constructor.
List<String> l1=new ArrayList<String>(); //taken String type for demonstration purpose.
List<String> l2=new ArrayList<String>(); //taken String type for demonstration purpose.
l1.add("A");
A.Data test=new A.Data(l1, l2);
return test;
}
public static void main(String[] args){
//for demonstration purpose accessing member variable directly instead of
//getter & setter.
System.out.println(getData().createdObjects);
}
Output:
[A]
As per comments, if you want to access createdObjects
member variable of Data
you can do it like below:
class A{
.. //other code
public static void main(String[] args) {
System.out.println(getData().getCreatedObjects());
//OR
System.out.println(getData().createdObjects);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | hfontanez |
Solution 2 |