InspectionGadgets request

Initialization Issues: Instance variable may not be initialized. Can you
add a check box to ignore primitives?

--
Norris Shelton
Web Developer
Sun Certified Java Programmer



6 comments
Comment actions Permalink


Easy enough to do, but I'd love to know your reasoning. How's an uninitialize primitive any less dangerous than an uninitialized reference. Just NPE's?

0
Comment actions Permalink

int startNum;
Defaults to a valid value, 0.


Object startObject;
Defaults to a null, which can cause a NPE.

--
Norris Shelton
Web Developer
Sun Certified Java Programmer


"Dave Griffith" <dave.griffith@cnn.com> wrote in message
news:2678419.1074884309077.JavaMail.itn@is.intellij.net...
>

Easy enough to do, but I'd love to know your reasoning. How's an

uninitialize primitive any less dangerous than an uninitialized reference.
Just NPE's?


0
Comment actions Permalink

That's only true for member variables. A variable declared inside a method
body is in an unitialised state until it is explicitly assigned a value.
This is true for primitive types and object references. If your code
attempts to read a variable while it's in this state you'll get a
compilation error. Try compiling the following, if you want an example:

class Example {
public void example() {
int a;
System.out.println("a = " + a);
}
}

Hope that helps,
Vil.

Norris Shelton wrote:

int startNum;
Defaults to a valid value, 0.


Object startObject;
Defaults to a null, which can cause a NPE.


--
Vilya Harvey
vilya.harvey@digitalsteps.com / digital steps /
(W) +44 (0)1483 469 480
(M) +44 (0)7816 678 457 http://www.digitalsteps.com/

0
Comment actions Permalink

Correct, this is for member variables. I should have clarified.

--
Norris Shelton
Web Developer
Sun Certified Java Programmer


"Vilya Harvey" <vilya.harvey@digitalsteps.com> wrote in message
news:bv38jj$48v$1@is.intellij.net...

That's only true for member variables. A variable declared inside a method
body is in an unitialised state until it is explicitly assigned a value.
This is true for primitive types and object references. If your code
attempts to read a variable while it's in this state you'll get a
compilation error. Try compiling the following, if you want an example:

>

class Example {
public void example() {
int a;
System.out.println("a = " + a);
}
}

>

Hope that helps,
Vil.

>

Norris Shelton wrote:

>

int startNum;
Defaults to a valid value, 0.

>
>

Object startObject;
Defaults to a null, which can cause a NPE.

>

>

--
Vilya Harvey
vilya.harvey@digitalsteps.com / digital steps /
(W) +44 (0)1483 469 480
(M) +44 (0)7816 678 457 http://www.digitalsteps.com/



0
Comment actions Permalink

Reasonable, although I personally prefer to have member variables explicitly initialized. If I see "private int m_foo = 0;", it means the developer actually intended it to be initialized to 0. If I see "private int m_foo;" he might have just forgot to initialize it, accidentally removed his initialization. Nonetheless, it seems a place that people can differ, so I'll add the switch.

--Dave

0
Comment actions Permalink

Thanks.

--
Norris Shelton
Web Developer
Sun Certified Java Programmer


"Dave Griffith" <dave.griffith@cnn.com> wrote in message
news:21571962.1075138220895.JavaMail.itn@is.intellij.net...

Reasonable, although I personally prefer to have member variables

explicitly initialized. If I see "private int m_foo = 0;", it means the
developer actually intended it to be initialized to 0. If I see "private
int m_foo;" he might have just forgot to initialize it, accidentally removed
his initialization. Nonetheless, it seems a place that people can differ,
so I'll add the switch.
>

--Dave



0

Please sign in to leave a comment.