简体   繁体   中英

Why can't we create instance of Collections class (not Collection Interface)?

Collections is a public class, then we can call its implicit default constructor. It doesn't have private constructor, which would prevent object creation or force to have static factory method. When I do instantiate as new Collections() , i get error as "Constructor not visible". In short why can't we have instance of java.util.Collections class? Thanks.

From the documentation : "This class consists exclusively of static methods that operate on or return collections."

In other words, Collections is just a collection of methods. An instance of it would not make any sense. It is just like the math functions: You don't have an instance of math, you just use the functions.

It is not an interface as it has concrete methods.

The reason for the "Constructor not visible" message is that the constructor is private (line 73), or at least according to this site . And as others already stated, what would you do with an instance of this class as it only contains static methods

// Suppresses default constructor, ensuring non-instantiability.
private Collections() {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM