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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:01:16+00:00 2026-05-11T07:01:16+00:00

EDIT: As of Java 8, static methods are now allowed in interfaces. Here’s the

  • 0

EDIT: As of Java 8, static methods are now allowed in interfaces.

Here’s the example:

public interface IXMLizable<T> {   static T newInstanceFromXML(Element e);   Element toXMLElement(); } 

Of course this won’t work. But why not?

One of the possible issues would be, what happens when you call:

IXMLizable.newInstanceFromXML(e); 

In this case, I think it should just call an empty method (i.e. {}). All subclasses would be forced to implement the static method, so they’d all be fine when calling the static method. So why isn’t this possible?

EDIT: I guess I’m looking for answer that’s deeper than ‘because that’s the way Java is’.

Is there a particular technological reason why static methods can’t be overwritten? That is, why did the designers of Java decide to make instance methods overrideable but not static methods?

EDIT: The problem with my design is I’m trying to use interfaces to enforce a coding convention.

That is, the goal of the interface is twofold:

  1. I want the IXMLizable interface to allow me to convert classes that implement it to XML elements (using polymorphism, works fine).

  2. If someone wants to make a new instance of a class that implements the IXMLizable interface, they will always know that there will be a newInstanceFromXML(Element e) static constructor.

Is there any other way to ensure this, other than just putting a comment in the interface?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T07:01:16+00:00Added an answer on May 11, 2026 at 7:01 am

    Java 8 permits static interface methods

    With Java 8, interfaces can have static methods. They can also have concrete instance methods, but not instance fields.

    There are really two questions here:

    1. Why, in the bad old days, couldn’t interfaces contain static methods?
    2. Why can’t static methods be overridden?

    Static methods in interfaces

    There was no strong technical reason why interfaces couldn’t have had static methods in previous versions. This is summed up nicely by the poster of a duplicate question. Static interface methods were initially considered as a small language change, and then there was an official proposal to add them in Java 7, but it was later dropped due to unforeseen complications.

    Finally, Java 8 introduced static interface methods, as well as override-able instance methods with a default implementation. They still can’t have instance fields though. These features are part of the lambda expression support, and you can read more about them in Part H of JSR 335.

    Overriding static methods

    The answer to the second question is a little more complicated.

    Static methods are resolvable at compile time. Dynamic dispatch makes sense for instance methods, where the compiler can’t determine the concrete type of the object, and, thus, can’t resolve the method to invoke. But invoking a static method requires a class, and since that class is known statically—at compile time—dynamic dispatch is unnecessary.

    A little background on how instance methods work is necessary to understand what’s going on here. I’m sure the actual implementation is quite different, but let me explain my notion of method dispatch, which models observed behavior accurately.

    Pretend that each class has a hash table that maps method signatures (name and parameter types) to an actual chunk of code to implement the method. When the virtual machine attempts to invoke a method on an instance, it queries the object for its class and looks up the requested signature in the class’s table. If a method body is found, it is invoked. Otherwise, the parent class of the class is obtained, and the lookup is repeated there. This proceeds until the method is found, or there are no more parent classes—which results in a NoSuchMethodError.

    If a superclass and a subclass both have an entry in their tables for the same method signature, the sub class’s version is encountered first, and the superclass’s version is never used—this is an "override".

    Now, suppose we skip the object instance and just start with a subclass. The resolution could proceed as above, giving you a sort of "overridable" static method. The resolution can all happen at compile-time, however, since the compiler is starting from a known class, rather than waiting until runtime to query an object of an unspecified type for its class. There is no point in "overriding" a static method since one can always specify the class that contains the desired version.


    Constructor "interfaces"

    Here’s a little more material to address the recent edit to the question.

    It sounds like you want to effectively mandate a constructor-like method for each implementation of IXMLizable. Forget about trying to enforce this with an interface for a minute, and pretend that you have some classes that meet this requirement. How would you use it?

    class Foo implements IXMLizable<Foo> {   public static Foo newInstanceFromXML(Element e) { ... } }  Foo obj = Foo.newInstanceFromXML(e); 

    Since you have to explicitly name the concrete type Foo when "constructing" the new object, the compiler can verify that it does indeed have the necessary factory method. And if it doesn’t, so what? If I can implement an IXMLizable that lacks the "constructor", and I create an instance and pass it to your code, it is an IXMLizable with all the necessary interface.

    Construction is part of the implementation, not the interface. Any code that works successfully with the interface doesn’t care about the constructor. Any code that cares about the constructor needs to know the concrete type anyway, and the interface can be ignored.

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

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, you're certainly doing it in a radically different way… May 11, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer Since you mention high-level operations on matrices and vectors, ATLAS,… May 11, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer Here's one solution, using COALESCE() to set the value of… May 11, 2026 at 8:05 pm

Related Questions

EDIT: As of Java 8, static methods are now allowed in interfaces. Here's the
I've been experimenting with various bits of Java code trying to come up with
I wanted to make Map of Collections in Java, so I can make something
Whenever I try to write graphical programs (whether a game or really any GUI
I'm experimenting with internationalization by making a Hello World program that uses properties files

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.