Not Initialized

Another in the riddle me this segments. I came across this, it's not a big deal but I wonder about it. following are 2 classes, one compiles, one doesn't.

// This one compiles

publicclass testNon{

publicint test;

publicint test2;

public Integer test3;

public testNon(){

test = test;

test2 = 55 + test;

test3 =new Integer(test);

}

publicstaticvoid main(String args[]){

System.out.println("Start");

testNon T =new testNon();

System.out.println("Test = " + T.test);

System.out.println("Test2 = " + T.test2);

System.out.println("Test3 = " + T.test3.intValue());

System.out.println("End");

}

}

// This one doesn't

publicclass testNon{

publicint test;

publicint test2;

public Integer test3;

/** Creates new testNon */

public testNon(){

test = test;

test2 = 55 + test;

test3 =new Integer(test);

}

publicvoid getID(){

int I;

I += 2;

}

publicstaticvoid main(String args[]){

System.out.println("Start");

testNon T =new testNon();

System.out.println("Test = " + T.test);

System.out.println("Test2 = " + T.test2);

System.out.println("Test3 = " + T.test3.intValue());

System.out.println("End");

}

}

In the first class the test int is not set to anything but when the class is instatiated, the test int initializes to 0.

In the second class, trying to use an int in a method where the int has not been initialized won't compile. The error is

variable I might not have been initialized

Just curious. Why does the instantiation of the object auto initialize the int (and other primitives I assume) and the method doesn't?

Thanks for a great forum.

[3532 byte] By [pauliemcneil] at [2007-9-27 18:39:52]
# 1
Instance variables are auto-initialized if you forget to do it yourself. Local variables are not... such as the one found in your method. Just the way the language is.
Smarsh at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 2
Regardless of context or scope of variable, always, always, always assign a value to a variable before use even if you don't have to.
andyba at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 3
If an instance variable is an object, say Integer(), is it also auto initialized and if so, where can I look to find which objects / primitives are auto initialized and to what value?Thanks againPaulie
pauliemcneil at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 4

> If an instance variable is an object, say Integer(),

> is it also auto initialized and if so, where can I

> look to find which objects / primitives are auto

> initialized and to what value?

>

> Thanks again

> Paulie

All instance variables are initialized... if they are primitives they are set to zero or "" if it is a string. All objects get their default constructors called.

Smarsh at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 5
Not entirely correct primitives are initialised to 0, except for boolean which is initialised to false. Objects are initialised to null.
serlank at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 6
True.In a quick test I just ran though, an object other than String, like Integer and Boolean will NOT auto initialize. It will compile BUT when you ask for the value you will get a nullPointerExeption.
pauliemcneil at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 7

> All instance variables are initialized...

This is not a requirement of the JLS in all cases and cannot be relied upon when not required

>if they are

> primitives they are set to zero or "" if it is a

> string.

Strings are objects, in situation where a default is called for then null is used for all objects regardless of class.

> All objects get their default constructors

> called.

No, there is no guarantee that a class has a default constructor available to be called, see the answer to your previous statement about default String assignment

andyba at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 8
well , i tried to declare an array , initialize it , but it still gave me an error . .int numbers[] = {0};numbers[0] = 0;how do i fix this ?
baahr at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 9
no , i had it like this :int numbers[] = {};numbers[0] = 0;or how should i properly make a new dynamic array ?
baahr at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...
# 10
Why don't you use an existing class like Vector, and make its contents int's?
TDXkev at 2007-7-6 19:43:55 > top of Java-index,Archived Forums,Java Programming...