DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2006

    final variables in method


    A method can have final and non-final variables in java.
    1.Where these(final&non-final) variables are stored in JVM?
    2.what is the life time of final variables of a method?
    3.Can we access final variables of a method out side of that method, if so how? Give me example and detailed explanation.

    Wating for your answers....

  2. #2
    Join Date
    Dec 2006
    final variables are replaced with the actual value wherever it is used during compilation itself.
    1. final int a=20,b=30;
    2. int c ;
    3. c = a + b;

    In the third line, since a & b are final variables, the compiler will replace this statement as
    c = 20 + 30;
    So, since this is part of method definition, it doesn't require separate memory location. (This will be stored in method area as part of method definition).

    This can lead to a bug.
    See the following scenario..
    If we have 2 different classes in two source file, and the first one is using a final variable of second class, and if you change the value of final variable of the second class, and if we didn't recompile the first class, still the first class will hold the previous value.

    Non final variables have local scope and it stores in stack of JVM.
    2. All local variables have life time only inside the block where it defines (including final).
    3. No local variables (including final) can access out side the block where it is defined.

Similar Threads

  1. TicTacToe help...
    By MP35719 in forum Java
    Replies: 0
    Last Post: 12-16-2006, 06:06 PM
  2. Replies: 5
    Last Post: 05-20-2002, 12:35 PM
  3. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM
  4. Replies: 0
    Last Post: 04-26-2001, 10:01 PM
  5. Replies: 3
    Last Post: 04-13-2001, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.