• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final variable

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]
 
Kapil Sakhuja
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic