Thanks for help in advance. I am a beginner of Java. I am using a class and defines its constructors. The 1st version is wrong with comments inside the code:
public class QueueByStacks {
// constructor
public QueueByStacks() {
LinkedList<Integer> in = new LinkedList<Integer>();
LinkedList<Integer> out = new LinkedList<Integer>();
}
public Integer poll() {
move();
return isEmpty()? null : out.pollFirst();
// out can not be resolved. But I think out is defined in the constructor then when I call the constructor in the main function then it should be initialized, so why I can not use out here?
}
}
I modified the code and it works:
public class QueueByStacks {
private LinkedList<Integer> in;
private LinkedList<Integer> out;
// constructor
public QueueByStacks() {
in = new LinkedList<Integer>();
out = new LinkedList<Integer>();
}
}
So I am wondering why the 1st version is wrong? My understanding is that when I call a class actually I am calling the constructor so "in" and "out" should be able to be used across the methods. I appreciate any helps. Thanks.