Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java initial assignment

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

like image 740
ddavison Avatar asked Sep 04 '14 14:09

ddavison


People also ask

What does initialize () Do Java?

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.

Is assignment an initialization?

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.

How do you initialize in Java?

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.

What is the difference between initialization and assignment Java?

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.


2 Answers

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;
like image 193
djechlin Avatar answered Sep 19 '22 00:09

djechlin


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.

like image 28
biziclop Avatar answered Sep 22 '22 00:09

biziclop