
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we have a private method or private static method in an interface in Java 9?
Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.
Syntax
interface <interface-name> { private static void methodName() { // some statements } private void methodName() { // some statements } }
Example
interface Java9Interface { public abstract void method1(); public default void method2() { method4(); method5(); System.out.println("Inside default method"); } public static void method3() { method5(); // static method inside other static method System.out.println("Inside static method"); } private void method4() { // private method System.out.println("Inside private method"); } private static void method5() { // private static method System.out.println("Inside private static method"); } } public class PrivateStaticMethodTest implements Java9Interface { @Override public void method1() { System.out.println("Inside abstract method"); } public static void main(String args[]) { Java9Interface instance = new PrivateStaticMethodTest(); instance.method1(); instance.method2(); Java9Interface.method3(); } }
Output
Inside abstract method Inside private method Inside private static method Inside default method Inside private static method Inside static method
- Related Articles
- Can we override a private or static method in Java
- Can we use private methods in an interface in Java 9?
- Can we declare an abstract method, private, protected, public or default in java?
- Why do we need private methods in an interface in Java 9?
- Can we have a constructor private in java?
- Can we declare a main method as private in Java?\n
- Can we declare interface members as private or protected in java8?
- Can we declare main() method as private or protected or with no access modifier in java?
- Can we declare an abstract method final or static in java?
- Can we declare the variables of a Java interface private and protected?
- What are the rules for private methods in an interface in Java 9?
- What are the advantages of private methods in an interface in Java 9?
- Why can't we define a static method in a Java interface?
- Default method vs static method in an interface in Java?
- Can we overload or override a static method in Java?\n

Advertisements