Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 760151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:40:59+00:00 2026-05-14T15:40:59+00:00

Hi I was going through the SCJP book about the innerclasses, and found this

  • 0

Hi I was going through the SCJP book about the innerclasses, and found this statement, it goes something like this.

A method local class can only refer to the local variables which are marked final

and in the explanation the reason specified is about the scope and lifetime of the local class object and the local variables on the heap, but I am unable to understand that. Am I missing anything here about final??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:40:59+00:00Added an answer on May 14, 2026 at 3:40 pm

    The reason is, when the method local class instance is created, all the method local variables it refers to are actually copied into it by the compiler. That is why only final variables can be accessed. A final variable or reference is immutable, so it stays in sync with its copy within the method local object. Were it not so, the original value / reference could be changed after the creation of the method local class, giving way to confusing behaviour and subtle bugs.

    Consider this example from the JavaSpecialist newsletter no. 25:

    public class Access1 {
      public void f() {
        final int i = 3;
        Runnable runnable = new Runnable() {
        public void run() {
          System.out.println(i);
        }
        };
      }
    }
    

    The compiler turns the inner class into this:

    class Access1$1 implements Runnable {
      Access1$1(Access1 access1) {
        this$0 = access1;
      }
      public void run() {
        System.out.println(3);
      }
      private final Access1 this$0;
    }
    

    Since the value of i is final, the compiler can “inline” it into the inner class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After going through the Appendix A, C# Coding Style Conventions of the great book
I was going through the AWDR book on web development with ruby on rails
I'm going through SICP and I'd like to have an interpreter analogous to the
I was going through the Programming Interviews Exposed book. There's a code given for
I was going through this: http://code.google.com/apis/webfonts/ (Which BTW I think is fantastic) And was
While going through university and from following the development of SO, I've heard a
I'm going through the problems on projecteuler.net to learn how to program in Erlang,
We're going through a massive migration project at the minute and trying to validate
I'm going through MSIL and noticing there are a lot of nop instructions in
I was going through some code and came across a scenario where my combobox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.