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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:17:49+00:00 2026-06-10T22:17:49+00:00

I have a class that I can’t change as follows: public class Foo {

  • 0

I have a class that I can’t change as follows:

public class Foo {
    public final int ID=0;
    public int bar;
    public final Object baz=null;
}

I want to have an anonymous inner class that overrides Foo such as:

public Foo newFoo(final int mID, final int mbar, final Object mbaz) {
    return new Foo(){
        public final int ID = mID;
        public int bar = mbar;
        public final Object baz = mbaz;
    };
}

I then have a javax.script.ScriptEngine that I want to call something like this in JS: newFoo(0,0,undefined)["ID"]

My problem is that I get an exception like this:

sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.IllegalAccessException: Class sun.org.mozilla.javascript.internal.JavaMembers can not access a member of class Baz$1 with modifiers "public final" (mod.js#9) in test.js at line number 9

I don’t understand how I could be getting a problem here: this works just fine if newFoo returns a plain ‘ol Foo, but if I try to override Foo with an anonymous inner class, I can’t access any overridden members even if they’re public.

  • 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-06-10T22:17:51+00:00Added an answer on June 10, 2026 at 10:17 pm

    I think there are two aspects to this problem.

    Your Java code is syntactically valid (I think), but it almost certainly does not do what you want it to do. Specifically, the declarations in the anonymous class don’t replace the declarations in the Foo class. Instead, each instance of the anonymous class gets two sets of fields. The methods declared in Foo will only see the original fields, and any methods declared in the anonymous class will see the new fields.

    If you want to have different values for the fields in the anonymous subclass, then:

    • they cannot be final, and
    • they need to be assigned to not redeclared.

    I can’t really advise the best way to solve this, but I think you will need to start by making the Foo fields private, and providing getters and/or setters for them. In the case of the setters, you can use access modifiers to allow the anonymous subclass to set the field, but not unrelated code.

    The bottom line is that you cannot override fields in Java. What your code is doing is not overriding, it is hiding the fields.


    The other aspect is the wrapped exception is coming from the Javascript engine. I think that the problem is that anonymous classes are implicitly private, and that your Javascript snippet is therefore trying to access a field on a private class. When the Javascript engine tries to do this (using Java reflection, I expect), it gets a runtime exception.

    The only unfortunate thing here is that the exception message doesn’t say why it can’t access the member, but your Java code is sufficiently “strange” that this doesn’t surprise me.

    As a work-around, I’d suggest replacing the anonymous class with a non-nested class.

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

Sidebar

Related Questions

If I have a class that can't be changed (inside a jar), Ex. public
Let me explain best with an example. Say you have node class that can
I have a class that I can have many instances of. Inside it creates
I have a Base class that needs a Generic type. That can be either
Here's the scenario: I'd like to have a host class that can have a
I have a class in C++ that I can't modify. However, that class holds
I have a class which contains a static collection that can be used across
In a client application I have a DBIx::Class model 'Todo' that can be linked
I have a simple class with an attribute that can contain a list of
I have a class Something that implements ISomething . How can I convert/cast from

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.