I'm using Eclipse and JFrame to create a program. I need to pass the value of a variable type INT from one class to another, but when I try to do it, the value of the variable within the frame
of the second class is 0
.
class Father:
public class PAdre extends JFrame {
/**
* Global
*/
private int id_pelicula = 2;
/**
* Create the frame.
*/
public Padre() {
setTitle("Clase Padre");
setBounds(100, 100, 628, 410);
JPanel contentPane = new JPanel();
setContentPane(contentPane);
btnVerReferencias = new JButton("Ver Ref.");
btnVerReferencias.setEnabled(false);
btnVerReferencias.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
claseHijo classHijo = new claseHijo ();
// Paso el valor de la variable
classHijo.setIdPelicula(id_pelicula);
classHijo.setVisible(true);
}
});
btnVerReferencias.setBounds(12, 288, 110, 74);
contentPane.add(btnVerReferencias);
}
}
class Son:
public class classHijo extends JFrame {
/**
* Global
*/
private int id_pelicula;
// Recibo el valor de la variable
public void setIdPelicula(int id_pelicula) {
this.id_pelicula = id_pelicula;
}
/**
* Create the frame.
*/
public classHijo () {
setTitle("Clase Hijo");
setBounds(100, 100, 628, 410);
JPanel contentPane = new JPanel();
setContentPane(contentPane);
// Le asigno el valor de la variable global
int otraVariableINT = id_pelicula;
System.out.println("otraVariableINT : " + otraVariableINT);
}
}
Exit:
otraVariableINT: 0
Why is the variable id_pelicula
worth 0
within frame
of the class Son? It is assumed that this variable is global and maintains its value throughout the execution of the program no?.
What is the correct way to pass a value to another class and that is available to use within frame
, what do I do wrong?