In my previous years of development in C++, I know that it was a convention to always initialize your variables with something.
I've carried that convention with me to Java, and really any other programming language I use.
I use IntelliJ IDEA, and I am completely happy with it, and usually follow its programming guidelines and warnings, however I got this warning:
public String getText(By by) {
String text = null; // Variable `text` initializer `null` is redundant
WebElement ele = findElement(by);
highlightIfDemoMode(ele);
String tagName = ele.getTagName();
if (tagName.equals("input") ||
tagName.equals("select"))
// For field elements, the text is actually held in values, not the text.
text = ele.getAttribute("value");
else
text = ele.getText();
return text;
}
Now obviously, in Java,
String text;
// is the *exact* same thing as..
String text = null;
Considering IntelliJ is very good at following programming conventions, should I neglect that convention of mine to not initialize my variables like this in Java? I feel it's much cleaner, but if Java conventions tell me not to, i won't.
Could somebody tell me if it's better practice to initialize, or just infer that it's null
To initialize a variable is to give it a correct initial value. It's so important to do this that Java either initializes a variable for you, or it indicates an error has occurred, telling you to initialize a variable. Most of the times, Java wants you to initialize the variable.
What is the difference between initialization and assignment? Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.
Initializing Instance Members Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods. The Java compiler copies initializer blocks into every constructor.
initialization: to initialize a variable. It can be done at the time of declaration. assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.
One compiler difference is
String text;
text.toString();
Is a compile error.
String text = null;
text.toString();
compiles, but is a runtime error.
Compile errors are better than runtime errors. Not declaring your variable until you have a value for it is better than either, but sometimes impossible.
int x;
try(SQLRunner runner = new SQLRunner(connectionPool) {
x = runner.getX();
}
Is necessary. Consider making your variables final in this case, however. This is good practice when possible anyway, but I personally find it too verbose and not helpful to declare every local variable that will not change as final. This is an exception because int x;
tells me it's going to be changing later without telling me how much it's going to change.
final int x;
According to Effective Java it's bad practice to declare and initialise local variables before their first use. (And according to me too but I'm not much of an authority.)
The idea is to keep their scope as tight as possible, this improves readability and stops accidental re-use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With