Why isn't Java
allowing me to initialize final variables indirectly? I don't see why it shouldn't work, I mean the method will always run so what difference does it make if initialized directly or via method?
This code works:
package com.company;
public class Person {
private final String name;
public Person() {
name = "bob";
}
}
This one doesn't. (even if I get rid of IO code and hardcode the value assigned to name)
package com.company;
import java.util.Scanner;
public class Person {
private final String name;
public Person() {
askName();
}
public void askName() {
Scanner scanner = new Scanner(System.in);
name = scanner.nextLine();
}
}