Today, I made a serious error, which is allocating the memory of reference variable repeatedly.

However, if I did it, the complier will throw a error. Why did I create two references that have the same names but the complier didn’t throw the error?

The reason is that I create a reference outside the method and the other reference that has the same name was created inside the method. This may be fatal. Because the first reference would be hided while the program was running in the block/method and created the other total different variable but they had the same name.

1
2
3
4
5
6
7
8
9
10
11
public class Calc extends JFrame implements ActionListener{

JTextField t1;

Calc(){
init();
....
}

void init() {
JTextField t1=new JTextField(10); //error