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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:48:10+00:00 2026-06-04T00:48:10+00:00

I have a Java problem with nested classes. My first class structure looked like

  • 0

I have a Java problem with nested classes.

My first class structure looked like this:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }            

    private interface NestedClass {
        public void method();
    }    

    private class NestedClass1 {
        public void method() {
        }
    }    

    private class NestedClass2 {
        public void method(){
        }
    }    
}

But now I want these method() methods to be static because they should be principally.

I cannot make them static without having them in a static class, but that’s no problem, I made the classes static, they should be anyway.

It looks like this right now:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }            

    private static interface NestedClass {
        public void method();
    }    

    private static class NestedClass1 {
        public static void method() {
        }
    }    

    private static class NestedClass2 {
        public static void method(){
        }
    }    
}

But then the trouble begins. A static method does not inherit correctly from a non-static interface method, as I get this message This static method cannot hide the instance method from TopClass.NestedClass in Eclipse.

When I make the interface method static, it gives me this error: Illegal modifier for the interface method method; only public & abstract are permitted

So I thought of an abstract class, and tried this:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }    

    private static abstract class NestedClass {
        public static abstract void method();
    }    

    private static class NestedClass1 {
        public static void method() {
        }
    }    

    private static class NestedClass2 {
        public static void method(){
        }
    }    
}

But again, seemingly abstract methods cannot be declared static: The abstract method method in type NestedClass can only set a visibility modifier, one of public or protected.

Leaving the static away (in the abstract class method), errors this on the method methods in the NestedClass1 & 2: This static method cannot hide the instance method from TopClass.NestedClass.

Isn’t there any way to declare some kind of superstructure for covering static methods?

EDIT:
The problem I actually try to solve it the lack of possibility of Java for storing references to methods. So instead I have those classes everyone with just one method, but to store them in a List f.e. they must be able to be “caught” by a superstructure.
I got the hint to try anonymous classes or enums, gonna try that now.

  • 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-04T00:48:14+00:00Added an answer on June 4, 2026 at 12:48 am

    A static method declaration must always be followed by a definition. It cannot be implemented by subclasses.

    I think you’re just not approaching your problem right. Try a different approach!

    • Make NestedClass an interface NestedInterface and store your different implementations as anonymous classes implementing this interface:

      public static final NestedInterface firstNested = new NestedInterface() {
          @Override
          public void method() {
              // ...
          }
      };
      
    • Make NestedClass an enumeration NestedEnum and store your different implementations as enumeration values implementing an abstract method from the enumeration. This only works if you have a fixed number of implementations you which to choose from and you do not want to accept NestedClass implementations from outside sources.

      public enum NestedEnum  {
      
          FIRST {
              @Override
              public void method() {
                  // ...
              }
          };
      
          public abstract void method();
      }
      

    EDIT: In reply to your comment:

    The classes itself are static as well..

    static in the context of a nested class means that this class can be instantiated without an instance of the containing class.

    A regular nested class such as in your first example can be instantiated through TopClass.this.new NestedClass1(). Normally you’d simply write new NestedClass1() from within the constructor or an instance method of TopClass, but in this verbose form you can clearly see the dependence on TopClass.this. This can also be seen from any method of NestedClass1, as you have access to the containing class with TopClass.this.

    A static nested class such as in your second example can be instantiated through new TopClass.NestedClass1(). Once again, you could just write new NestedClass1() but the verbose form clearly shows that the construction only depends on TopClass and is not associated with an instance of TopClass. You could even create an instance from an outside class using the same snippet new TopClass.NestedClass1() without ever creating a TopClass instance.

    I suggest you take a look at this question on inner classes and static nested classes.

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

Sidebar

Related Questions

I have a structure with the following domain classes: class Service{ static hasMany=[serviceRequirements:ServiceRequirement]} And
I have a class like this : class User { String name; String password;
I have a problem with bounded nested wildcards in Java generics. Here's a common
I have some problem with the nested try statements in java and I am
I'm working on a project using Java RMI. This is the class causing problem:
I have next structure: @Component public abstract class HuginJob extends QuartzJobBean {...} @Component(CisxJob) public
I have DTO which contains itself (it is nested). public class MyDTO { private
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
So I have a problem where Java is telling me that the following line
I'm having a small problem in Java. I have an interface called Modifiable. Objects

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.