Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Implementation within the Interface - Java

We know that we can't implement functions in interfaces in java.

I just tried

public interface InvoiceService {
public static void getData(){
 System.out.print("this is my data");
 }
}

I am able to execute this function, why is it like that? is it because the function is defined as static and static variables can be accessed using class name directly without creating an object?

Answer*

Draft saved
Draft discarded
Cancel
5
  • 3
    Good answer - posting some code examples would improve youir chances of upvotes. Commented Sep 15, 2015 at 8:58
  • @OldCurmudgeon Nope. you can't do that in older versions. Commented Sep 15, 2015 at 8:59
  • @sᴜʀᴇsʜᴀᴛᴛᴀ - Oops! Thanks for the heads-up. Commented Sep 15, 2015 at 9:00
  • default method is another type of method that is allowed in interfaces in Java8. They contain a body and can be overridden by implemented classes. However static methods cannot be override. Commented Sep 15, 2015 at 9:04
  • @DanyalSandeelo Did you go through the official doc provided in my answer? Commented Sep 15, 2015 at 9:04

lang-java

AltStyle によって変換されたページ (->オリジナル) /