Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Wed Jan 08, 2014 11:47 am
by JeramieH
Since bool is an instance variable, why isn't it automatically defaulted to false? Does the final flag prevent default values?

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Wed Jan 08, 2014 9:06 pm
by admin
It is initialized to false automatically, but as per JLS 17.5:
An object is considered to be completely initialized when its constructor finishes. A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.

Therefore, if you don't initialize it explicitly, you can't use it in or before the constructor.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Thu Feb 20, 2014 2:53 pm
by srao.nagaraj
The following also declares a boolean instance variable that is final as well and the following code does not fail compilation
but if I comment the throw new IOException(); in the constructor this fails compilation. can you please clarify on why is this behavior.

This seems to be defeat the following condition, isnt it?
A final variable must be initialized when an instance is constructed, or else the code will not compile.


class InitChild
{
final boolean bool;
static
{
System.out.println("Inside InitChild.static initializer" );
}

{
System.out.println("Inside InitChild.instance initializer" );
}

InitChild() throws IOException{

throw new IOException();
//System.out.println("Inside InitChild.constructor" + i);
}

public static void main(String[] args) throws IOException
{
InitChild i = new InitChild();
//System.out.println("Value of bool is "+i.bool);

}




}

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Thu Feb 20, 2014 8:45 pm
by admin
A final variable must be initialized before it is used for the first time. If you are not using it anywhere, then it can be left uninitialized.

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Fri Feb 21, 2014 8:43 am
by srao.nagaraj
Admin,

Thanks for looking.

But this code block below does not compile. In this case bool is not initialized and not used as well.
The Only change that i did was to uncomment throw new IOException(); from my previous example.

Any inputs on this please?

class InitChild
{
final boolean bool;
static
{
System.out.println("Inside InitChild.static initializer" );
}

{
System.out.println("Inside InitChild.instance initializer" );
}

InitChild() throws IOException{

//throw new IOException();
//System.out.println("Inside InitChild.constructor" + i);
}

public static void main(String[] args) throws IOException
{
InitChild i = new InitChild();
//System.out.println("Value of bool is "+i.bool);

}




}

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Fri Feb 21, 2014 12:52 pm
by admin
Well, I was wrong earlier. As per Section 8.3.1.2: http://docs.oracle.com/javase/specs/jls ... ls-8.3.1.2
A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.
Now, when you have throw new IOException(); in the constructor, the constructor never completes and so the compiler is ok with bool not being initialized.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Thu Jun 12, 2014 2:43 am
by Chandni
I just had a doubt. when final boolean bool was written as a class variable, it needed initialization but when it was declared as a local variable it complied without any error. It did not need initialization. Why so ?

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Thu Jun 12, 2014 5:20 am
by admin
Local variables don't need to be initialized if they are not used anywhere.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Thu Aug 14, 2014 12:37 pm
by vchhang
Just a thought but you may want to either add another question similar to this but with bool declared as static and final. I notice for static and final variables, it must be initialized in a static block. I had this question earlier.

Re: About Question enthuware.ocajp.i.v7.2.1346 :

Posted: Thu Aug 14, 2014 9:51 pm
by admin
vchhang wrote:Just a thought but you may want to either add another question similar to this but with bool declared as static and final. I notice for static and final variables, it must be initialized in a static block. I had this question earlier.
Thank you for the suggestion but such a question already exists. It explains that static and final variable must be initialized. enthuware.ocajp.i.v7.2.1243

HTH,
Paul.