0

References can be created using

Class_Name Var_Name;
Class_Name Var_Name=null;

I know that in both the cases Var_Name is assigned null value . Is it just a good coding standard to assign null(nullify the reference) for a reference or is there any logic or advantage of assigning null.

JOHND
  • 2,597
  • 6
  • 27
  • 35
  • 2
    Are we talking fields in the class or variables inside a method? – Anthony Grist Jul 01 '13 at 10:52
  • Can you please tell me what will happen in both the case – JOHND Jul 01 '13 at 10:53
  • This depends on whether you write it as a class field or as a local variable within a method. The first one will trigger a compiler when you use it as a local variable and you try to read the variable value before you assign the value. – Simon Forsberg Jul 01 '13 at 10:53

5 Answers5

2

It is not just a good coding.

You have to initialize variables in Java. For fields of classes you don't have to do that, but in fact they are initialized as null.

For local variable you cannot use uninitialized variable like this:

String n;
n.length()

Also found similar question: uninitialized vars

Community
  • 1
  • 1
Tala
  • 8,888
  • 5
  • 34
  • 38
  • I mean if suppose initially i declare ref variable as Test t; and after lot of calculation and operation i assign value t=somethingObj; what will happen – JOHND Jul 01 '13 at 10:57
  • you cannot use t until it is initialized (talking about local variables). so you can do that t = something, if you have never used t before – Tala Jul 01 '13 at 10:58
  • If the compiler can tell that `t` will be initialized before its use, then the compiler won't complain. But, the compiler's analysis is not particularly robust. `boolean b = true; if(b) {t=new Test();} t.exec();` will not compile. – Eric Jablow Jul 01 '13 at 11:01
  • but what i should prefer in that case – JOHND Jul 01 '13 at 11:01
  • always set it to null, except for cases like this: String s; if () s="YES" else s="NO". Compiler won't complain since s WILL definitely be initialized – Tala Jul 01 '13 at 11:04
2

Well, if the above declaration is at instance level , then assigning null explicitly is redundant as instance variables get their default values. For local variables however you should initialize the varible before you use it, else there would be an compiler error

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • I mean if suppose initially i declare ref variable as Test t; and after lot of calculation and operation i assign value t=somethingObj; what will happen – JOHND Jul 01 '13 at 10:57
  • In that case `t` will now point to the newly created object(somethingObj) and reference to your initial object will be lost – PermGenError Jul 01 '13 at 10:58
  • In both the case or in second case?? – JOHND Jul 01 '13 at 11:04
  • As i said, it depends, if its an instance varibale it doesn't really make any difference, in case of local variables however, in order to do those "Calculations" you need to initialize your local variable. – PermGenError Jul 01 '13 at 11:06
  • Thanks PermGenError, but can you please clarify if in both the cases later i can assign an object to that instance why i should prefer Test t= null;(I am talking about local variables) – JOHND Jul 01 '13 at 11:32
1

Initializing fields to 0-false-null is redundant.

Explicitly setting fields to 0, false, or null (as the case may be) is unnecessary and redundant. Since this language feature was included in order to, in part, reduce repetitive coding, it's a good idea to take full advantage of it. Insisting that fields should be explicitly initialized to 0, false, or null is an idiom which is likely inappropriate to the Java programming language.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

In the case of class fields, there's no difference at all. Fields that aren't explicitly initialised are initialised with the default value for that type when an instance of the class is created; in the case of objects, the default is null. So:

private MyClass myClass1;

is equivalent to

private MyClass myClass1 = null;

since both will result in myClass1 being null.

In the case of method variables, there's a pretty large difference. Variables have to have a value declared, even if that's null, otherwise you'll get (potential) compiler errors when you later try to use it. Doing something like this:

public void myMethod() {
    MyClass myClass1;

    if(someCondition)
        myClass1 = new MyClass();

    myClass1.doSomething();
}

would be invalid, since the compiler can't guarantee that myClass1 will have a value. Changing that line to MyClass myClass1 = null; would be valid, though in practice you could get a runtime error (a NullPointerException) if someCondition isn't true.

You could do something like this:

MyClass myClass1;

// calculate some values

myClass1 = new MyClass(arg1, arg2, ...);

but that seems pointless to me; there's no need to have myClass1 mentioned until you're instantiating it. Instead just do:

// calculate some values

MyClass myClass1 = new MyClass(arg1, arg2, ...);
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
  • Nice explanation and line "but that seems pointless to me; there's no need to have myClass1 mentioned until you're instantiating it. Instead just do:" – JOHND Jul 01 '13 at 13:14
0

Depends on your use case.

For variable level variables if you don't assign null, you get a compiler error if you use the variable before assigning value to it.


For class level variables, it's just redundant.

darijan
  • 9,725
  • 25
  • 38