I've read some other questions about declaring objects inside loops, like:
Is it Better practice to Declare individual objects or loop Anonymous objects into ArrayList?
Java : declaring objects in a loop
but neither really address my question.
I'm scanning for user input repeatedly and creating a class to parse that string every iteration:
public static void main(String[] args) {
while (true) {
System.out.print("Enter a string of brackets to test: ");
String exp = new Scanner(System.in).nextLine().trim();
if (exp.equals("q")) break; // q is the 'quit' flag
System.out.println(new BracketMatcher(exp.length()).parse(exp));
}
}
Is there any difference - in performance, not scope - to this block?:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BracketMatcher matcher;
while (true) {
System.out.print("Enter a string of brackets to test: ");
String exp = input.nextLine().trim();
if (exp.equals("q")) break; // q is the 'quit' flag
matcher = new BracketMatcher(exp.length());
System.out.println(matcher.parse(exp));
}
Would I be better off making parse() a static method in BracketMatcher since I only use that method?
Thanks.