This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable" Watch "final variable" New topic
Author

final variable

Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
class a{
final int i=10;
public static void main(String s[])
{
a ob=new a();
ob=null;
System.out.print(ob.i);
}}

First code is working,as static as well as final both
are loaded at the time when class is loaded so why
second code is not working.

class a{
final int i=1;
int j=i*k;
final int k=3;
public static void main(String h[])
{
a ob=new a();
System.out.print(ob.j);
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kapil,
Order is important in how you declare variables. In your example you are trying to use variable k before the compiler knows what it is. The final keyword doesn't change the loading order. Static variables are loaded when the class is loaded in the order in which they are declared. Instance class variables are loaded when an instance is created in the order in which they are declared.
Regards,
Manfred.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234

class a{
final int i=1;
int j=i*k; // k is not defined yet so how can u use it

final int k=3;
public static void main(String h[])
{
a ob=new a();
System.out.print(ob.j);
}
}[/B]

sona<br />SCJP
Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
Then Why is the First Code working?

Originally posted by Manfred Leonhardt:
Hi Kapil,
Order is important in how you declare variables. In your example you are trying to use variable k before the compiler knows what it is. The final keyword doesn't change the loading order. Static variables are loaded when the class is loaded in the order in which they are declared. Instance class variables are loaded when an instance is created in the order in which they are declared.
Regards,
Manfred.

Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
then Why is the First Code Working?
Originally posted by sona nagee:

class a{
final int i=1;
int j=i*k; // k is not defined yet so how can u use it

final int k=3;
public static void main(String h[])
{
a ob=new a();
System.out.print(ob.j);
}
}

[/B]
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
The first one works because when
final int i = 10 is executed there are no undefined variables.
The trick is that the compiler needs to know what a variable holds before you can use it.
final int i = 10;
int j = i*k; //up to this point we know that i = 10
//but what is k set to? It is unknown.
//Keep in mind as stated earlier the order that
//things get set. As said earlier,
//instance variables are executed in the order that
//they are declared.
Try something like this vs. what not having the variable k defined:
final int i = 10;
int k = 5;
int j= i*k;
Rajesh Patil
Greenhorn

Joined: May 21, 2001
Posts: 5
How can the variable 'i' in the first code be accessed if
the object refering to it has been assigned to null?
ie. ob=null;
ob.i; // why is nullpointer exception not thrown?

[This message has been edited by Rajesh Patil (edited May 22, 2001).]
[This message has been edited by Rajesh Patil (edited May 22, 2001).]


rptl
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186

Hi, i find the above code here, compiled and run it , find it fine.
Well, i find following segment in JLS

15.11.1 Field Access Using a Primary
The type of the Primary must be a reference type T, or a compile-time error occurs. The meaning of the field access expression is
determined as follows:
If the identifier names several accessible member fields of type T, then the field access is ambiguous and a compile-time error occurs.
If the identifier does not name an accessible member field of type T, then the field access is undefined and a compile-time error occurs.
Otherwise, the identifier names a single accessible member field of type T and the type of the field access expression is the declared type of
the field. At run time, the result of the field access expression is computed as follows:
If the field is static:
If the field is final, then the result is the value of the specified class variable in the class or interface that is the type of the Primary expression.
If the field is not final, then the result is a variable, namely, the specified class variable in the class that is the type of the Primary expression.
If the field is not static:

If the value of the Primary is null, then a NullPointerException is thrown.
If the field is final, then the result is the value of the specified instance variable in the object referenced by the value of the Primary.

If the field is not final, then the result is a variable, namely, the specified instance variable in the object referenced by the value of the Primary.

Since the variable intended (i.e i )is not a static one, i think the evaluation of the reference (i.e ob) take place first and a NullPointerException should be thrown.
What did i miss?
Thanks in advance
James
[This message has been edited by James Du (edited May 24, 2001).]
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Ranchers,
James has correctly reffered to the approriate JLS specification.


If the value of the Primary is null, then a NullPointerException is thrown.
If the field is final, then the result is the value of the specified instance variable in the object referenced by the value of the Primary.


In view of specification the answer that should be printed would be "10" and it would NOT BE a compile time error. The reason is that since the variable is final the compiler knows that its value cant change throughout the execution of the program so it can treat it as a constant and do optimisation on the same.
On the other hand if declare the variable as "static" then the variable will no longer be a instance variable but rather it would be class variable so it's value will be retained even after the object is set to "null".
Hope this clears the doubt.
Ravindra Mohan.
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi Ravindra
Since the specification give the check order, i think it should first check if the reference is null or not before it look whether the field is final or not, if the specification give the order in another way, i would agree with you, but...
Maybe the specification was not organized well enough ?
Regards
James
[This message has been edited by James Du (edited May 24, 2001).]
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi James,
I agree with you, but my talisma is when in doubt, rely on the compiler. The compiler didn't complain and printed the
value "10". Hence, I had to contend to what JLS says. The order
does confuse me , but that how we have to contend ourselves
Hope this is fine now.
Ravindra Mohan.
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Since you do get a Runtime Exception (NullPointerException) if i is not final, we'd have to conclude that the documentation is ambiguous at best and incorrect at worst in suggesting that final variables are not checked until after it is verified that the Primary is not null. Reversing the order of those statements:

If the field is final, then the result is the value of the specified instance variable in the object
referenced by the value of the Primary.
(implied else) If the value of the Primary is null, then a NullPointerException is thrown.

would seem to be more accurate.
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi all
What I worried is whether it's sensible for us to jump to the conclusion so quickly? You're certain that nothing was missed out?
Through i should not underestimate our sense of judge, i still dont believe that JLS is so ill-expressed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable
 
Similar Threads
e below program is throwing error at compile time --- showVal(int a) can not applied to ()
ExamLab toArray
private variable scope
scjp2
compilation fails why?