Results 1 to 7 of 7
Thread: about Final variable
- 08-16-2010, 04:55 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 11
- Rep Power
- 0
about Final variable
First Part:
=======
1) If I wrote two classes like this
public class First{
public final int i=10;
}
public class Second{
public static void main(String[] args){
First first=new First();
System.out.println("print i value--->"+first.i);
}
}
2) then compile the code with
c:> javac Second.java
and run the code
c:> java Second then the output will be
print value---->10
Second Part:
==========
3) Now change i value from 10 to 20 in class First.
public final int i=20;
4) Now compile only First.java only (Ex: c:> javac First.java)
5) Now run Second.class only (Ex: c> java Second)
Now the output will be print value---->10
But If do the above steps when I have "public int i=10;" in First.java class
then for First part I am getting "print value---->10" as a result
and for Second part I am getting "print value---->20" as a result.
My doubt is why the result is 10 in second part when the variable is declared as final?
- 08-16-2010, 12:03 PM #2
final int i, means you can't change that variable value.
when int i =10;
we can do : i = i+5; // from other methods
but this can't be done, when final int i =10;
If some variable needs to be static through out application then use final.
final (Java) - Wikipedia, the free encyclopedia
- 08-17-2010, 01:18 AM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Yeah but the OP isn't changing the variable at runtime. He said he putfinal int i, means you can't change that variable value.
when int i =10;
we can do : i = i+5; // from other methods
but this can't be done, when final int i =10;
public final int i = 20; and when he compiled and referenced, it was 10.
Hmm, I just tested, and OP didn't mess up, final does something weird.
Extra point: if you recompile Second, it will print the new value 20.
Ok, so if it is final, it will look for the value only at compile, and if it isn't, it will look at runtime.Last edited by collin389; 08-17-2010 at 01:40 AM.
- 08-17-2010, 04:58 AM #4
Member
- Join Date
- Aug 2010
- Posts
- 11
- Rep Power
- 0
- 08-17-2010, 05:58 AM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Becuase final means that it won't change during runtime, so the JVM doesn't check to see if the variable has changed because it doesn't expect it to.
- 08-17-2010, 06:33 AM #6
Last edited by Prajin; 08-17-2010 at 06:36 AM. Reason: Some mispell.
- 08-17-2010, 09:49 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
The compiler treats final variables as literal constants and uses them as such: i.e. if class A contains a final variable which is used in class B and class A is recompiled afterwards, the compiled code for class B still uses the old value for the final variable; class B isn't recompiled automagically.
kind regards,
Jos
Similar Threads
-
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
local variable tf1 is accessed from within inner class; needs to be declared final
By shoeb83 in forum New To JavaReplies: 2Last Post: 12-05-2009, 11:24 AM -
[SOLVED] is final class members are also final ?
By haoberoi in forum New To JavaReplies: 4Last Post: 11-10-2008, 03:01 PM -
Final int
By Fireking in forum New To JavaReplies: 10Last Post: 09-05-2008, 07:12 AM -
Poi 3.0-final
By levent in forum Java SoftwareReplies: 0Last Post: 05-22-2007, 07:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks