aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializing final arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Initializing final arguments" Watch "Initializing final arguments" New topic
Author

Initializing final arguments

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
I was trying to write the below code

import java.util.*;

public class InitializeFinalArgs
{
final int i;


public InitializeFinalArgs()
{
}


}

It was giving compile time error as saying int i is not initialized
but when i modified the above code as

import java.util.*;

public class InitializeFinalArgs
{
{final int i;}


public InitializeFinalArgs()
{
}


}

Its not giving any error.
What going on here. Somebody please explain me.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

In the second case the i is declared inside the initializer block. So it is accessible only inside the initializer. So it is not compulsory to initialize it.



But you must initialize a final field which is accessible at the class level. That is why the first code is giving an error...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
The fact is that final variables (whether instance or static) do not get default values from the compiler. For a final static variable, you have to initialize it in its declaration, or in a static initialization block. For a final instance variable, you have to initialize it in its declaration, in an instance initialization block, or in the class constructor (in every constructor of the class, as a matter of fact.)
[ January 01, 2009: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Ruben,

I would like to share my experience related to static initialization of final variables.


if you run this test code

public class Test{
final static int var;


Test()
{
System.out.println("inside cons--"+var);
}

static
{
new Test();
var=22;

}

public static void main(String...arg)
{
System.out.println("in main=="+var);


}

}

Output is : 0
22
[ January 01, 2009: Message edited by: Jolly tiw ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Jolly where is your final variable ?


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Originally posted by Jolly tiw:
Ruben,

I would like to share my experience related to static initialization of final variables.


if you run this test code

public class Test{
final static int var;


Test()
{
System.out.println("inside cons--"+var);
}

static
{
new Test();
var=22;

}

public static void main(String...arg)
{
System.out.println("in main=="+var);


}

}

Output is : 0
22

[ January 01, 2009: Message edited by: Jolly tiw ]

Jolly,

That's very interesting. I don't quite know what is going on there. It would appear that the final variable is getting a default value of 0, but that defies logic. If you remove the initialization to 22, the code won't compile. Would you mind expanding on your understanding of what is going on?

Also, the fact that you can call a constructor from a static initialization block of the same class is very strange. I really do not understand what is going on there.
[ January 02, 2009: Message edited by: Ruben Soto ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I think it is some type of trick, as see this code:
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
@punit and rouben

It seems as compiler ensures this thing only that we could not refer a final static variable before initializing it but if there are any references to that variable inside a static function or it is referred inside constructor and call to any of them is before that initialization , In both the cases such variables will take their default values.

This test code shows this


public class Test{
final static float var;

Test()
{
System.out.println("inside cons--var1==="+var);
}

static
{
fun1(); //or new Test();
var=22.5;

}

static void fun1()
{

System.out.println("inside fun1--"+var);
}

public static void main(String...arg)
{


}


}
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

You don't need a static field to observe this behavior. This is also applicable on non-static final fields

camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
always that you use final to variable you have to put on one value to them, this is one rule.
then:

the code above compile normally.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Punit and Jolly,

Thanks for clarifying that. Viewed as a special case or "trick" it makes sense. It doesn't mean the final variable is assigned a default value however. I instead view it as the compiler replacing any use of that variable in the context you mentioned by a default value, but not assigning that default value to the variable, since that is impossible (if that were the case, then all final variables would only be able to hold their default values, since you can only assign a value to a final variable once.)

Ankit,

I see that what you say is true, but I think the mechanism is a little different. Non-final variables do actually get assigned default values. I think what is going on with final variables is a little different, and it's what was leading to a lot of confusion.

Thanks, guys.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben, Ankit is talking about non-static final, not non-final variable.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Punit Singh wrote:Ruben, Ankit is talking about non-static final, not non-final variable.

Indeed, Punit. Sorry for misreading your post, Ankit!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Initializing final arguments
 
Similar Threads
please answer my query?
code
Cannot get code with assertions to compile
Import statement problems
doubt in treeset...