Static class vs singleton class

I know this topic was discussed and killed over and over again, but I still had one doubt that I was hoping someone could help me or direct me to a previously existing post on SO.

In traditional C, static variables are stored in data segments, and local variables are stored on the stack. I would suggest that static variables would be more expensive to store and maintain compared to local variables. Correctly?

When you try to understand from a Java or C # perspective, will this be a drawback for static classes compared to a singleton class? Since the whole class is loaded into memory before the class is initialized, I do not see how this can be an advantage if we do not have small built-in functions.

I like the Singleton classes, and I would not want this to become an anti-pattern, I am still looking for all the benefits that come with it ... and then lost the thread safety argument among others.

-Ivar

+5
java singleton
source share
4 answers

Unlike C, the static in the definition of a Java class simply means that it is a regular class like any other class, but it is simply declared inside another class to organize the code. In other words, there is no behavioral difference between the following two methods of declaring *:
but)

 class SomeOtherClass { static class Me { // If you "upgrade" me to a top-level class.... } } 

b)

 class Me { // I won't behave any different.... } 

Class definitions are loaded into memory when the class is used for the first time, and this is true for the static and non-static classes. There is no difference in how memory will be used. In older JVMs, objects were always stored on the heap. Modern JVMs allocate objects on the stack whenever possible and useful, but this optimization is transparent to the encoder (it is impossible to influence this behavior using code), and the use of the static does not affect this behavior.

Now, back to the original question, as we saw, we really cannot compare static classes and Singleton in Java, since they are completely different concepts in Java (I'm also not sure how static classes would compare to Singleton, but I will focus on Java in this answer). The static in Java is overloaded and has many meanings, so it can be confusing.

Is Singleton automatically an “anti-pattern”? I do not think so. Singleton's abuse is that, but the Singleton pattern itself can have many good uses. It just happens that a lot of abuse. If you have a legitimate reason to use the Singleton template, there is nothing wrong with using it.



* Note. Why write static at all, you may ask. It turns out that the “non-static” nested classes have their own somewhat complicated memory management functions, and its use is usually discouraged unless you have a good reason (see Other questions for more information).
 class SomeOtherClass { Stuff stuff; class Me { void method(){ // I can access the instance variables of the outer instance // like this: System.out.println(SomeOtherClass.this.stuff); // Just avoid using a non-static nested class unless you // understand what its use is! } } } 
+7
source share

The Singleton class is essentially a regular top-level class with a private constructor to ensure its uniqueness. The Singleton class itself provides a way to capture its instance. Single classes are not very easy to test, so we tend to stick with the idea of Just Create Once .

static class is essentially a nested class. A nested class is essentially an outer level class that is nested in another class just for the convenience of packaging. A top-level class cannot be declared static , at least in Java - you must try it yourself.

Would this be an inconvenience to static classes compared to a singleton class?

Now this question has become somewhat invalid, according to the above explanation. In addition, the static class (of course, nested) can also be single.

Further reading:

  • Inner class in vs interface in class
+3
source share

The differences between one and the other are memory management, if your application has to create many things that burn memory, like a charm becoming a memory problem, performance and other things ... it can help ...

http://butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne http://www.objectmentor.com/resources/articles/SingletonAndMonostate.pdf

0
source share
0
source share

All Articles