Here is an extract from the JLS3 - Sec 12.4.1 Order of execution of the initializers is one of the things to consider here. Having them could only mean more work for the compiler? I think we can safely say that because of the above, the static blocks are not required in an interface. The following two styles must be functionally equivalent ( not really sure on this one - not sure if inlining works with both, but it must be an implementation detail ) though the first one must be more concise and readable. For the former, in an interface the static variables are already also final variables. So they don't apply.Ī static block is generally used to initialize variables that should be initialized only once or to call a static method. The sorting util methods is present in the Collections Class as a static method.įrom Java 8 onwards, since Interfaces can have static methods, it makes the design more cohesive by keeping these static util methods inside List itself.Could this be one another way to put it? Just my reasoning - if you think it's incorrect, please point out.įirst we only have instance method declarations in an interface for obvious reasons - implementation is deferred to an implementing class that declares to keep the contract. This is how any list was sorted till Java 7. Until Java 7, the general practice was to have such util methods in a separate utility class which contains all the static methods which can be invoked on an interface's implementation's instance.įor example: Collections class has a sort method - static void sort(List list, Comparator c). So, in case you need any util methods which can operate on an Interface's implementing class's instances, then add that as a static method to the Interface itself. Listed below are the main diftferences in the way static and default methods are used in interfaces -ĭifference 3 - Purpose served in the overall design When to design\use Static Methods: Static Methods are the utility(util) methods which are associated to an Interface. Java 8 Static Methods vs Default Methods in Interfaces In case you are not familar with default methods, please refer the detailed article I have written on them here - default methods tutorial Tutorial on Java 8 Default Methods. As their name suggests these methods are available by default to all the classes which implement this interface. These are methods written in Interfaces, indicated by the modifier default. Note - This is same as the way static methods are accessed on a Class. įor example: Interface has a static method isEqual() which can be accessed like this Predicate.isEqual(). Static methods in interfaces are accessible through the interface name like this. Beginning Java 8 static methods are now allowed in Interfaces as well. Till Java 7, static methods were only allowed on Classes. Quick Introductions to Static & Default Methods What are Static Methods: These are methods written in Interfaces which are static. On the contrary, static and default methods work together to deliver the full functionality of an interface. Its not like you have to choose one of them to implement. An important point to understand before we start looking at static and default methods in interfaces is that they are not an either/or options. This article explains Java 8 Static Methods and Default Methods in Interfaces w.r.t to their various aspects with examples.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |