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 1006525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:28:31+00:00 2026-05-16T08:28:31+00:00

How to access i from the outer class? HashSet<Integer> hs=new HashSet<Integer>(){ int i=30; };

  • 0

How to access i from the outer class?

  HashSet<Integer> hs=new HashSet<Integer>(){
        int i=30;
    };

I can do it like this

int k=new HashSet<Integer>(){
    int i=30;
}.i;

But if I get ‘i’ then I cannot get the instance of hashset.Is there a way to get both?The question just out of curiosity.It doesn’t have much practical applications.I just want to know whether it can be done.

  • 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-16T08:28:32+00:00Added an answer on May 16, 2026 at 8:28 am

    Solution 1: Object registration system

    The two snippets presented essentially made a choice between storing the reference to the anonymous class instance just created, or to immediately access its custom field. Doing one would seem to forfeit the ability to do the other.

    One way to achieve a compromise is to have an object registration system, and use an instance initializer (JLS 8.6) in your anonymous class to self-register to this sytem.

    Here’s a simple proof-of-concept, using Object[] for a simple registration system:

        final Object[] registrar = { null };
        int k = new HashSet<Integer>(){
            { registrar[0] = this; }
            int i= 666;
        }.i;
        Set<Integer> set = (Set<Integer>) registrar[0];
    
        System.out.println(k); // 666
        System.out.println(set.getClass().isAnonymousClass()); // true
    

    Essentially we use a 1-element Object[], and in the instance initializer of our anonymous class, we “register” this to this array. Note that this is just a proof-of-concept; in production code, you’d use a more robust and typesafe object registration system than this.


    Solution 2: Tuples

    This approach works nicely if your anonymous class may have multiple “fields”. Simply pack them all into a tuple, making sure to include a reference to this in the anonymous class.

    Here’s a simple proof-of-concept, using Object[] for a tuple:

        Object[] tuple = new HashSet<Integer>(){
            Object[] tuple = { this, 666, 13, "XXX", "OMG ABUZE" };
        }.tuple;
        Set<Integer> set = (Set<Integer>) tuple[0];
        set.add(null);
        System.out.println(set.getClass().isAnonymousClass()); // true
        System.out.println(Arrays.toString(tuple));
        // [[null], 666, 13, XXX, OMG ABUZE]
    

    In production code, you’d use a CustomTuple class that is more object-oriented and typesafe. Note that this is an analogous solution to the “how can my method return 2 values” problem: simply return 1 value that captures all of those information.

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

Sidebar

Related Questions

So most of us know how to access an outer class from an inner
I am trying to access the outer class variable cell[i][j] from the inner class
just a very simple thing, is there a way to access outer class from
I would like to use an SQLite database from Microsoft Access (access from- or
I need to access camera from a remote html page. I load this html
How can we access variable from one Xaml file in to another xaml file
Is it possible to get a reference to this from within a Java inner
When defining nested classes, is it possible to access the outer class' methods? I
I'm trying to access variables from an inner class of a deserialized json object.
If I have an instance of an inner class, how can I access the

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.