Hello all
general question is i like to build logger class that writes to single log file
from different classes in my application what should the logger class be
singletone or static class ... |
|
I know this topic has been discussed and killed over and over again, but I still had one doubt which I was hoping someone could help me with or guide me ... |
what is need of singleton(state is fixed ) class if I have class with static methods (fixed behaviors )?
|
When is a singleton class preferred over a class that has only static methods and a private default constructor?
Please vote.
|
Why do we need to make a class singleton when we can achieve the same by declaring attributes and methods as static. When searched i found the benefits are: 1) Lazy loading of resources in singleton class. This seems to be fine. 2) Inheritance While i didn't still got cleared with inheritance as when i extends the inherited class the getInstance() ... |
Always make your methods static unless you can't. In other words you need access to the object's state. I highly recommend Effective Java. It covers topics such as defaulting to static methods, using singletons, and why both are considered excellent design principles. http://java.sun.com/docs/books/effective/ As far as your singleton versus static, they have nothing in common. Singleton's simplify maintenance of shared state. ... |
|
Regarding having a singleton class and having a static object in the class, which is the better way to implement. See i need to get the reference data from the database and store them in the hashtable. So whenever some method needs the data it will call that class and and if hashtable in not empty it will get the data ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |
Hi all. I have a web application that reads and writes from a text file. The different servlet request threads may need to read or write to/from this file, and so I want the read/writes to happen one at a time (synchronized that is), of course. Performance is not an issue in my particular case. It will only be used by ... |
If your singleton class implements an interface or extends another class, then without the singleton instance you will not be able to use your class as a parameter or return value. A good example is String.CASE_INSENSITIVE_ORDER - a singleton Comparator so you can use it in Collections.sort for instance. Another example is Collections.EMPTY_SET and its brothers - singleton Set, List etc ... |
In addition to the above difference, i would like to know the following : 1) What is class loading? When does it occure for a class that is executed using say a console using the java command. This question is important as i am trying to find answers to many doubts related to static initializer blocks. 2) Can you start ... |
Hi All, I am wondering why to create singleton class in application when same purpose (having single instance) can be served with a Class and all its methods as static, because even in this we donot need to create object to invoke methods . Can any body put light on this point? |
|
|
Rudramoorthy wrote: The singleton pattern is useful to ensure that 'only one instance of that class' is ever created and available during the running of your application. A static method is only useful for utility methods where they dont need to access instance variables or maintain state True, I agree on that. But one instance or one class; there will be ... |
Hi I wonder what could be the difference between static methods and singleton? In singleton at any point of time there will be only one object existing in the JVM. We create singleton to avoid multiple object creation for that class. If I make all the methods in the class as static also it does the same job. The memory will ... |
I am quite aware of the singleton pattern. And also aware of the concept of static class. Since static class methods and variables can be accessed without creating object and it can be used from anywhere in the application. Then why do we go for singleton pattern which almost works the same way. Any light thrown on this is appreciated |
You can view a class with all static methods as a form of singleton, though I expect purists will say it's not because a singleton is supposed to be an instance of a class. In Java, the only real difference is that with a "true" singleton, you can have runtime polymorphism. That is, you could write your client code to call ... |
Unloading the class isn't exactly the problem. The problem is that your URLClassLoader isn't loading the class. What it is doing is first delegating to its parent, which is the standard classloader. Since that classloader searches the classpath, it finds your Singleton class and loads it. Job done. Then the second time you do that, your new URLClassLoader delegates to the ... |
first answer to your question is, you cant declare class as a static. you can declare methods as a static no issues but if you want only one instance of ur class to be created through out the application then you go for singleton class, keep in mind also that if u make ur class as a singleton then its advised ... |