Skip to main content
Software View

Main navigation

  • Home
  • Trainer Profile
  • Blog
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Blog

Accessing local variables even after methods finish (in Java)

By Kamal Wickramanayake, 3 December, 2010

At times we write methods in Java classes with local variables that we need to access even after the methods finish execution. Here's an example:

 

public class FinalLocalVariableTest {

    public static void main(String[] args) {
        final int x = 5;

        Runnable r = new Runnable() {            
            @Override
            public void run() {
                System.out.println("x is " + x);
            }   
        };

        Thread t = new Thread(r);
        t.start();
    }
}

Note that within the inner class 'x' is being accessed which is a local variable. Since 'r' is passed to a new Thread, this Runnable created may live even after the method (in this case the main() method) finishes.

In Java, memory is allocated for local variables in frames associated with the method execution stack. When a method finishes, the frame disappears and the memory allocated for local variables is no longer needed and is reused. But then, what happens to 'x'? We continue to access it within the Runnable even after the method finishes. 'x' should live even after the method execution frame is removed from the stack.

Java wants you to declare such local variables directly accessed within inner class code as final.

What happens when you start using the final local variable in an inner class is that the inner class gets a copy of the field. The inner classes in fact never access the method execution frame allocated variable. Read this:

"An anonymous local class can use local variables because the compiler automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each constructor to initialize these automatically created private fields. Thus, a local class does not actually access local variables, but merely its own private copies of them. The only way this can work correctly is if the local variables are declared final, so that they are guaranteed not to change. With this guarantee in place, the local class is assured that its internal copies of the variables accurately reflect the actual local variables."

So, that resolves the mystery of how an inner class continues to access a final local variable even after the method (that declares final local variables) runs into completion.

Reference: http://renaud.waldura.com/doc/java/final-keyword.shtml

Java

Highlights

  • O'Reilly Book "97 Things Every Software Architect Should Know" Accepts A Write Up From Kamal
  • "Service Oriented Architecture - Making IT Infrastructure Speaks Business" - Presentation At The ISACA 4th Annual Conference
  • The Second Bacth Of ICTA Nanasala e-Society Members Receives Trainings On HTML/CSS and GIMP
  • GIMP Training For ICTA Nanasala Project
  • Agile Processes Training For PPSL (Pvt) Ltd
  • Computer Society of Sri Lanka (CSSL) - Talk on "Introduction to IT Governance and Enterprise Architecture"
  • Motorola Sends A Second Batch Through Software Patterns Training
  • Kamal To Act As The Marketing Director - ISACA Sri Lanka Chapter
  • ISACA Sri Lanka Chapter Invites Kamal To Join As A Board Member
  • Epic Lanka Technologies (Pvt) Ltd Receives Java SE And Java EE Trainings From Software View
  • Patterns Training For PPSL (Pvt) Ltd
  • ISACA Members Day Presentation On "Introduction To IT Governance And Enterprise Architecture"
  • Opening Lecture On Information Technology For SLIDA Master Of Public Management Course Delivered By Kamal
  • Customized Java Enterprise Edition Training For SLIDA
  • No One To Beat Software View - Epic Lanka Technologies (Pvt) Ltd
  • Motorola Receives Software Patterns Training From Software View
  • Custom Java Enterprise Edition Training for ICTA/SLIDA - Only from Software View!
  • Java EE 5, JavaServer Faces, Hibernate And Spring For PPSL (Pvt) Ltd
  • "Brain Trust" For Linux Journal Weekly Newsletter From Kamal
  • Java Platform, Enterprise Edition 5 Training At The CEIT, University Of Peradeniya
  • Another Group Of Around 100 Sri Lanka Telecom Engineers And Managers Were Service Oriented!
  • Java Platform, Enterprise Edition 5 Training Will Be Held At The CEIT, University Of Peradeniya
  • Service Oriented Architecture: Another Two Sessions Conducted at SLT
  • Photos of IET Monthly Forum at the Peradeniya University
RSS feed
Copyright © 2007 - 2023 Software View