Reputation: 1095
I've this class:
public class MenuUpElement {
Class<?> classe;
String label;
int viewId;
public MenuUpElement(int viewId, String label, Class<?> classe) {
viewId = this.viewId;
classe = this.classe;
label = this.label;
}
}
Then I have a static class StaticClass with this declaration:
public static final MenuUpElement[] menuUpElements = new MenuUpElement[]{
new MenuUpElement(12, "Main", MainActivity.class)
, new MenuUpElement(13, "Second", SecondActivity.class)
};
If I loop through StaticClass.menuUpElements in another class, I found two elements (correct) but all null (wrong):
menuUpElements[0].classe = null
menuUpElements[0].viewId= 0
menuUpElements[0].label= null
menuUpElements[1].classe = null
menuUpElements[1].viewId= 0
menuUpElements[1].label= null
Why?
Upvotes: 0
Views: 58
Reputation: 85779
The constructor of MenuUpElement
is wrong, you're setting the parameters with the values of the fields. It should be the other way around:
public MenuUpElement(int viewId, String label, Class<?> classe) {
this.viewId = viewId;
this.classe = classe;
this.label = label;
}
Upvotes: 1
Reputation: 30210
The assignments in your constructor are backwards
public MenuUpElement(int viewId, String label, Class<?> classe) {
viewId = this.viewId;
classe = this.classe;
label = this.label;
}
Consider
public MenuUpElement(int viewId, String label, Class<?> classe) {
this.viewId = viewId;
this.classe = classe;
this.label = label;
}
Upvotes: 6