I am new to java. When I was going through language specification I found that static classes cannot be declared, but we can have static inner classes. I am little confused why we cannot have a top level static class. If I want to create a class containing only utility methods, I can go for static class. Any reason why developers of Java restricted this feature ?

解决方案

It is not a restriction, you do not need static class to define a utility class, you only need static methods. For example the class Math in java is full of static methods, but the class itself is not static.

You might only need static class when you define an inner class that you want to use without creating an instance of the enclosing class, which is allowed in Java.

You can define your utility class as follows:

class Util {

public static void method(){

// your utility method

}

}

Logo

昇腾计算产业是基于昇腾系列(HUAWEI Ascend)处理器和基础软件构建的全栈 AI计算基础设施、行业应用及服务,https://devpress.csdn.net/organization/setting/general/146749包括昇腾系列处理器、系列硬件、CANN、AI计算框架、应用使能、开发工具链、管理运维工具、行业应用及服务等全产业链

更多推荐