About Question enthuware.ocajp.i.v7.2.1346 :
Moderator: admin
-
- Posts: 22
- Joined: Wed Jan 08, 2014 11:24 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1346 :
Since bool is an instance variable, why isn't it automatically defaulted to false? Does the final flag prevent default values?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
It is initialized to false automatically, but as per JLS 17.5:
Therefore, if you don't initialize it explicitly, you can't use it in or before the constructor.
HTH,
Paul.
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.
-
- Posts: 6
- Joined: Fri Jan 31, 2014 8:28 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
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);
}
}
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);
}
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
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.
-
- Posts: 6
- Joined: Fri Jan 31, 2014 8:28 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
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);
}
}
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);
}
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
Well, I was wrong earlier. As per Section 8.3.1.2: http://docs.oracle.com/javase/specs/jls ... ls-8.3.1.2
HTH,
Paul.
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.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.
HTH,
Paul.
-
- Posts: 6
- Joined: Thu May 08, 2014 12:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
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 ?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
Local variables don't need to be initialized if they are not used anywhere.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1346 :
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.1243vchhang 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.
HTH,
Paul.
Who is online
Users browsing this forum: No registered users and 6 guests