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

  • Home
  • SEARCH
  • 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 3978620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:04:14+00:00 2026-05-20T05:04:14+00:00

I’m working with some code where one object, foo, is creating another object, bar,

  • 0

I’m working with some code where one object, “foo”, is creating another
object, “bar”, and passing it a Callable. After this foo will return
bar, and then I want foo to become unreachable (ie: available for
garbage collection).

My initial thought was to just create the Callable anonymously. eg:

class Foo {
  ...

  public Bar createBar() {
    final int arg1 = ...
    final int arg2 = ...
    final int arg3 = ...
    return new Callable<Baz>() {
      @Override
      public Baz call() {
        return new Baz(arg1, arg2, arg3);
      }
    };
  }
}

It occurred to me that this might not actually work as desired, however,
as an inner class typically keeps a reference to its enclosing object.
I don’t want a reference to the enclosing class here, because I want the enclosing object to be
collected while the Callable is still reachable.

On the other hand,
detecting that the enclosing instance is never actually referred to
should be pretty trivial, so perhaps the Java compiler is smart enough
to not include a reference in that case.

So… will an instance of an anonymous inner class hold on to a
reference to its enclosing instance even if it never actually uses the
enclosing instance reference?

  • 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-20T05:04:15+00:00Added an answer on May 20, 2026 at 5:04 am

    As of JDK 18, no. JDK 18 omits enclosing instance fields from inner classes that don’t use it.

    However, prior to JDK 18, yes, instances of anonymous inner classes hold on to a reference to their enclosing instances even if these references are never actually used. For example, this code:

    public class Outer {
      public Runnable getRunnable() {
        return new Runnable() {
          public void run() {
            System.out.println("hello");
          }
        };
      }
    }
    

    …when compiled with javac, generates two class files: Outer.class and
    Outer$1.class. Disassembling the latter, the anonymous inner class,
    with javap -c yields:

    Compiled from "Outer.java"
    class Outer$1 extends java.lang.Object implements java.lang.Runnable{
    final Outer this$0;
    
    Outer$1(Outer);
      Code:
       0:   aload_0
       1:   aload_1
       2:   putfield        #1; //Field this$0:LOuter;
       5:   aload_0
       6:   invokespecial   #2; //Method java/lang/Object."<init>":()V
       9:   return
    
    public void run();
      Code:
       0:   getstatic       #3; //Field java/lang/System.out:Ljava/io/PrintStream;
       3:   ldc     #4; //String hello
       5:   invokevirtual   #5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8:   return
    
    }
    

    The putfield line shows that a reference to the enclosing instance is
    being stored in the field this$0 (of type Outer) by the constructor
    even though this field is never used again.

    This is unfortunate if you’re attempting to create small potentially
    long-lived objects with anonymous inner classes as they’ll hold onto the
    (potentially large) enclosing instance. A workaround is to use an instance of a static class (or a top-level class) instead. This is unfortunately more verbose.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.