6

Here's part of a small program I'm doing as a homework assignment:

public Exam maxGrade() {
    Node p = firstNode;
    int max = 0;
    String name;
    while (p!=null) {
        if (p.info.getGrade()>max) {
            max = p.info.getGrade();
            name = p.info.getName();
        }
        p = p.next;
    }

    if (name==null)
        return null;
    else
        return searchByName(name);
}

So when I go ahead and compile, the compiler outputs this message:

Student.java:127: error: variable name might not have been initialized if (name==null)

The problem is easily solved by substituting the fourth line with:

String name = null;

Now, I can see some logic in this. But I'd really like to grasp the workings behind the problem. I mean, it seems reasonable that the compiler checks whether a variable is initialized if it sees you're doing something with it in your code, but I don't think I'm doing anything that NEEDS the variable to be initialized.

According to sources like this when I simply declare my String (or any other Object) variable "name", it already points to null. Then why is it considered an anomaly to simply check if that value is null? Will the compiler consider an error anything that I do to my uninitialized variable other than assignments?

Community
  • 1
  • 1
user2397616
  • 93
  • 1
  • 1
  • 6
  • 3
    No, that's *not* what it (the link you provide) says. As stated in the accepted answer, Local variables are not initialized, hence the error. – Brian Roach May 18 '13 at 20:38
  • possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) among numerous others. – Brian Roach May 18 '13 at 20:39

3 Answers3

14

Fields of object type are initialized to null by default. Array members are also initialized to null by default.

Local variables are not - they must be initialized explicitly.

This is a good thing. Uninitialized variables are frequently an indication of error.

From "Initial Values of Variables" in chapter 4 of the Java Language Specification:

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 2
    Also note that null can cause some issues with certain string functions if you aren't careful. Thus, sometimes I do `String foo = ""` instead. – Chris Chambers May 18 '13 at 20:39
1

The compiler requires that you initialize the Object to be null if you're making any assumption as to its value. This is simply a (very useful) precaution.

onon15
  • 3,620
  • 1
  • 18
  • 22
0

Addendum:The compiler cant check the semantics of your program. Even if you know that a variable is initilized before its first usage the compiler cant.

consider the following function:

public boolean mod2(int i){
    if(i % 2 == 0){
        return true;
    }
    if(i % 2 != 0){
        return false;
    }
}

We all know that this method would always return true or false. The compiler instead cant ensure that there will always be a return value because he has to know that there will only those two results. So the compiler will report an error about a missing return statement.

user
  • 735
  • 1
  • 6
  • 21