Results 1 to 7 of 7
  1. #1
    Ramanjaneyulu is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default 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?

  2. #2
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    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

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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;
    Yeah but the OP isn't changing the variable at runtime. He said he put
    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.

  4. #4
    Ramanjaneyulu is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by collin389 View Post
    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.
    This is wondering me.anybody knows why it is happening to final variable only.

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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.

  6. #6
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Quote Originally Posted by collin389 View Post
    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.
    Hmm, only one thing comes in my mind, as OP compile First.java after making final int =20; but not Second.java, I think Second still have old value and showed 10.That's why when Second is compiled and run it gave new value.

    -Regards
    Last edited by Prajin; 08-17-2010 at 06:36 AM. Reason: Some mispell.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  1. Final int
    By Fireking in forum New To Java
    Replies: 12
    Last Post: 06-22-2013, 05:44 PM
  2. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  3. Replies: 2
    Last Post: 12-05-2009, 11:24 AM
  4. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM
  5. Poi 3.0-final
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-22-2007, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •