[英]Singleton class only has static fields and methods but has many instances in the memory
In our project, we often use a utility class and its static methods to load some icons. 在我们的项目中,我们经常使用实用程序类及其静态方法来加载一些图标。 This utility class is singleton .
该实用程序类为singleton 。
But in the running environment, we find 314 instances of this class, under which circumstances does that occur? 但是在运行环境中,我们发现314个此类的实例,在什么情况下会发生?
A static utility class isn't quite the same as a singleton class. 静态实用程序类与单例类不太相同。 (A singleton class is where there is exactly one instance and you access its instance methods, not static methods.) If you don't want instances of your class being created, make sure there is at least one declared constructor and that all constructors are declared private.
(在一个单例类中,只有一个实例,您可以访问其实例方法,而不是静态方法。)如果您不希望创建类的实例,请确保至少有一个声明的构造函数,并且所有构造函数都是宣布为私人。 All places where you are creating instances should then light up as compiler errors.
然后,您正在创建实例的所有位置都应作为编译器错误点亮。
当涉及多个类加载器时。
Could you post your implementation? 您可以发布您的实施方案吗? It sounds like you are instantiating the static class multiple times.
听起来您要多次实例化静态类。 Is your constructor private?
您的构造函数是私有的吗? Are you implementing the getInstance method correctly?
您是否正确实现了getInstance方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.