Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

References to interface static methods are allowed only at source level 1.8 or above

What do I need in order to solve this problem? How to change JRE Library in Eclipse Project?

import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class LabelGroup {
SortedSet<String> strSet1 = Stream.of("A", "B", "C", "D")
             .collect(Collectors.toUnmodifiableSet());
    
}

Eclipse shows at Stream.of:

References to interface static methods are allowed only at source level 1.8 or above

Tried to install new software from official site as suggested by wiki.

Response:

Could not find (the software).

About Eclipse: Eclipse IDE for Enterprise Java Developers Version: 2018-12 (4.10.0) Build id: 20181214-0600

Project Settings: java build path: JRE System Library [JavaSE - 1.7]

like image 975
Optimight Avatar asked Feb 25 '19 04:02

Optimight


People also ask

Are static methods allowed in interface?

Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.

Can we access a static method of an interface by using reference of the interface?

You can't access static methods of interfaces through instances. You have to access them statically. This is a bit different from classes where accessing a static method through an instance is allowed, but often flagged as a code smell; static methods should be accessed statically.

Why static methods are allowed in interface?

Java interface static methods are good for providing utility methods, for example null check, collection sorting etc. Java interface static method helps us in providing security by not allowing implementation classes to override them.

CAN interface have more than one static method?

Sure you can, but only with the newest version of Java i.e Java 8. Static method will be same as default method in interface but it cannot be overridden by the implementing class. Unlike static methods in class, these methods cannot be called using class objects. You will have to use interface name to call this method.


1 Answers

If you're working on a maven project and you're not able to change the JRE Library in Eclipse, then you can add the following dependency to the pom.xml

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
like image 163
Natasha Kurian Avatar answered Sep 17 '22 20:09

Natasha Kurian