Reputation: 21
How do you access inherited attributes in java outside the constructor?
public PainelPrincipal(Jogo jogo,GridPanel gridPanelPrincipal) {
super(jogo,gridPanelPrincipal);
listaBlocos = new LinkedList<>();
carregarFicheiroNivel();
gridPanelPrincipal.setEventHandler(this);
}
private void carregarFicheiroNivel() {
FileHandler handler = new FileHandler("/niveis/EstadoInicial.txt");
String conteudo = handler.readFile();
String[] colunas = null;
int y=0;
for(String linha: conteudo.split("\n")){
colunas = linha.split(" ");
for(int x = 0; x < colunas.length; x++) {
if(colunas[x].substring(1, 2).equals(PAREDE)){
grelha[x][y] = new Parede();
gridPanelPrincipal.put(0, 0, grelha[0][0].getCellRepresentation());
}else{
switch(colunas[x].substring(0, 1)){
case "0":break;
}
}
}
y++;
}
}
This line gridPanelPrincipal.put(0, 0, grelha[0][0].getCellRepresentation()); doesn't seem to work, he doesn't recognize the gridPanelPrincipal variable outside the constructor of the class.
Is it possible to access it outside the constructor or how can i do this?
Upvotes: 0
Views: 647
Reputation: 178333
You can't access it outside the constructor, because presumably it's private
. You can only access it in the constructor because it's an argument to the constructor.
You can either:
protected
in the superclass to make it visible to the subclass, ORGridPanel
, so you can access it in other methods.Upvotes: 1