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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:33:44+00:00 2026-05-28T07:33:44+00:00

I was playing around with anonymous subclasses and I found a problem that I

  • 0

I was playing around with anonymous subclasses and I found a problem that I can’t get around.

EDIT: problem is solved thanks to thomas, the complete working code is on pastebin (Foo.java; FooTest.java)

2 Classes; Foo and FooTest…
Code first:

class Foo {

    public Foo () {}

    public void whichClassAmI () {
        System.out.println(this.getClass());                                                 
    }

    public void say () {
        System.out.println("Foo says: nothing");                                             
    }   

    public <T extends Foo> T getAnotherFoo () {                                              

        try {                                                                                
            Class<? extends Foo> c = this.getClass();                                        
            T r = (T)(c.getConstructor().newInstance());                                     
            return r;                                                                        
        } catch (Exception e) {                                                              
            throw new RuntimeException(e);                                                   
        }
    }                                                                                        

}

_

class FooTest {                                                                              

    public static String stuff = "";                                                         

    public static void main (String[] args) {                                                

        Foo f1 = new Foo();                                                                  

        // instance of anon subclass                                                         
        Foo f2 = new Foo(){                                                                  
            public void say () {                                                             
                System.out.println("Modded Foo says: " + stuff);                             
            }                                                                                
        };                                                                                   

        f1.whichClassAmI();                                                                  
        f2.whichClassAmI();                                                                  

        stuff = "stuff";                                                                     
        f1.say();                                                                            
        f2.say();                                                                            

        Foo f3 = f1.getAnotherFoo();                                                         
        f3.say();                                                                            

        Foo f4 = f2.getAnotherFoo(); // <-- exception here
    }

}

So this code compiles with an unsafe operation warning, runs and throws an exception; output is:

Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
class Foo
class FooTest$1
Foo says: nothing
Modded Foo says: stuff
Foo says: nothing
Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: FooTest$1.<init>()
    at Foo.getAnotherFoo(Foo.java:20)
    at FooTest.main(FooTest.java:23)
Caused by: java.lang.NoSuchMethodException: FooTest$1.<init>()
    at java.lang.Class.getConstructor0(Class.java:2723)
    at java.lang.Class.getConstructor(Class.java:1674)
    at Foo.getAnotherFoo(Foo.java:17)
    ... 1 more

what I don’t understand is:

  1. f2 class is FooTest$1 and this class seems to be not extending Foo

  2. if (1) is true why does ” Class c = […] ” can be set with FooTest$1

  3. if (1) is false and (2) works correct, why doesn’t it find the method?

  • 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-28T07:33:44+00:00Added an answer on May 28, 2026 at 7:33 am

    To 1): f2 is of type FooTest$1 which extends Foo but that isn’t printed, you just get the current class and no superclasses or interfaces.

    To 2): Since 1) is false, there’s not question here 🙂

    To 3): The problem is that the annonymous inner class FooTest$1 needs an external FooTest instance to be created. Calling the constructor via reflection would try to create a new instance without an enclosing instance of FooTest and such a method is not available (i.e. a method that creates an instance of an inner class without an instance of the enclosing class).

    What you need to do is get the constructor that takes an instance of the enclosing class as its parameter. For more information have a look at this question: Is it possible to create an instance of nested class using Java Reflection?

    Edit:

    I read your code again and am ashamed I missed this: the FooTest$1 class is actually a static inner class since it is created in the static main method. Thus it doesn’t have a constructor taking an enclosing class instance.

    However, since you create the class and its constructor inline that constructor isn’t publicly visible. Thus getClass().getConstructor() won’t return it (this class will only return public constructors. In that case you have to use getClass().getDeclaredConstructor(). The same is true for fields and methods, just for the record.

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

Sidebar

Related Questions

Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
been playing around with ResolveClientUrl(~/Confirmation.aspx) and other methods.. I am tryin go get the
I'm playing around with the <canvas> element, drawing lines and such. I've noticed that
While playing around with the emulator, I noticed that when trying to view a
Just playing around with some of the APIs in .NET and I can't seem
Im playing around with the Tablesorter plug-in for jQuery and was trying to get
playing around with a property file i figured that there seems to be a
Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
Im playing around with the Devise invitable gem: https://github.com/scambra/devise_invitable It's working fine. But how
been playing around with Capistrano to get an automated deploy between my server and

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.