Difference between static class and singleton?

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 be allocated once. Even there wont be any object in this case, so time required for object creation also can be saved. looks like more efficient but still why need singleton?
This question is also posted in java programming forum. The thread is as follows:-
[Click on this to visit that thread |http://forum.java.sun.com/thread.jspa?threadID=5278517&tstart=0]
Edited by: murrayb9654 on Mar 25, 2008 8:20 AM

yawmark wrote:
Static class is a realization of singleton pattern.Not in any conventional sense.
~Especially since a static class is completely different from a class with all static methods.

Similar Messages

  • What is the difference between document class and normal class

    Hi,
    Please let me know what is the difference between document class and normal class.
    And I have no idea which class should call as document class or call as an object.
    Thanks
    -Actionscript Developer

    the document class is invoked immediately when your swf opens.  the document class must subclass the sprite or movieclip class.
    all other classes are invoked explicitly with code and need not subclass any other class.

  • What is the difference between Abstract class and Interface ?

    Hi,
    Could u plz tell me the difference between Abstract class and Interface?
    Thanks in advance.
    Gopi

    Lots.
    An abstract class can contain some method implementations, or indeed all the method implementations. It may contain methods with all the various access modifiers. It cannot be instantiated. A class may inherit from only a single abstract class.
    An interface contains only public method stubs and constants. A class may implement multiple interfaces. An interface cannot (obviously) be instantiated.
    Abstract classes are particularly useful when you need to provide a semi-complete implementation for reuse. Interfaces are used more like types.
    Look at java.util.* for some good examples of the use of both.

  • Difference between Abstarct Class and Interface

    HI,
    Here is a simple one for the gurus but quite important for me.
    What is the difference b/w an Interface and an abstract class?
    Why do we need each one of them?
    I would appreciate if you people can give examples of each so that I amy understand fully
    Thanks in advance...

    A normal class (not abstract) has a special behaviour, like java.awt.Frame. A Frame is a frame no matter how you subclass it. If you create a subclass it will still be able to be displayed by calling it's show method. So by using a normal class you can create a type of class.
    An abstract class does the same, but it leaves some of the code unwritten. For instance java.lang.Number. This class is abstract becuase it has no knowledge of how to store the number in memory. But it knows that it is a number, and any subclass will still be a number. You could create a subclass of Number that can hold the time in milliseconds and checks your system clock to see what the time is, it could also have methods that return the time in another country, but it would still be just a Number.
    An interface is a way to describe what an object can do, not what it is. So with java.lang.Comparable as an example you can make any class comparable. This means that no matter what type of object you have, it can be compared with other objects. So you can have a subclass of Frame that can be compared with other windows. Or a subclass of Number that can be compared with other Numbers. You can even compare those two different types if you like. So you could compare a window with a number.
    That is the difference between abstract classes and interfaces.
    I hope you could follow my arguments, it isn't an easy subject,
    Daniel

  • Difference between Data Class and Delivery Class

    What is the Difference between Data Class and Delivery Class , what happens Phisically to the Data .
    Moderator message: what is the difference between your question and a question that we'd welcome here in the forums?
    [Rules of engagement|http://wiki.sdn.sap.com/wiki/display/HOME/RulesofEngagement]
    Edited by: Thomas Zloch on Nov 22, 2010 1:17 PM

    What is the Difference between Data Class and Delivery Class , what happens Phisically to the Data .
    Moderator message: what is the difference between your question and a question that we'd welcome here in the forums?
    [Rules of engagement|http://wiki.sdn.sap.com/wiki/display/HOME/RulesofEngagement]
    Edited by: Thomas Zloch on Nov 22, 2010 1:17 PM

  • Difference between abstract classes and interfaces

    I actually wonder about what are the differences between abstract classes and interfaces may somebody give an example code about it?
    and i have one more question how can i use interfaces like multiple inheritance ? i mean when i implement an interface like
    class a extends b implements c,di have to use all c and d methods but what that methods means?
    I mean as i know we cannot make implementations of methods in interfaces
    but for example in runnable interface there is a method like run() and it has been defined somewhere because it knows what to do(i mean when it will run), i just write my code into that method .

    Once you get past the starting point (I am referring to the OP here), there are a few salient differences:
    You can only extend (or generalize) a single superclass; however, you can implement (or realize) multiple interfaces. As such, all things being equal, using an interface in lieu of an abstract class 'frees' your design. Later, if you want the implementor of an interface to inherit from another class, there is not issue.
    Any abstract method specifies a contract. However, abstract classes allow you to also add common behavior to subclasses. This is an overused justification for abstract classes, IMO. You can achieve the same effect using delegation and still having interfaces.
    Always program to interfaces wherever possible. This means that you define an interface and have an implementing class (usually at a minimum). Do not do this for all your classes, but rather the ones that make your system unique (the domain model or M in MVC architecture). This allows you to later change implementation with a minimal amount of refactoring. This is a core precept from the Group of Four and any number of decent programming books.Best of luck.
    - Saish

  • Difference between Material Class and Batch Class

    Hi!
    What is the difference between Material Class and Batch Class?
    and how can we use these classes?
    Rgds,
    Ajit

    Iam not sure but i think
    1) Material class for sorting purpose
    2) Batch Class for Batch Management
    thanks
    K.Prabakaran

  • Difference between Java class and JavaBean?

    What is the difference between a Java class and a JavaBean?
    A class has variables which hold state.
    A class has methods which can do things (like change state).
    And if I understand a JavaBean it is rather like a BIG class...
    i.e.
    A JavaBean has variables which hold state.
    A JavaBean has methods which can do things (like change state).
    So, what's the difference between the two?
    And in case it helps...What is the crossover point between the two? Is there a minimalist type JavaBean which is the same as a class? What is the difference between Java beans and Enterprise Java Beans?
    Thanks.

    Introspection, as I understand it is a bunch of
    methods which allows me to ask what a class can do
    etc. right? So, if I implement a bunch of
    introspection methods for my class then my class
    becomes a JavaBean?Introspection allows a builder tool to discover a Bean's properties, methods, and events, either by:
    Following design patterns when naming Bean features which the Introspector class examines for these design patterns to discover Bean features.
    By explicitly providing the information with a related Bean Information class (which implements the BeanInfo interface).
    I understand now they are completely different.
    Thanks. Very clear.
    I do not understand how they are completely different.
    In fact I don't have a clue what the differences are
    other than to quote what you have written. In your
    own words, what is the difference? This is the "New
    to Java Technology" forum ;-) and I'm new to these
    things.In that case ejbs are way too advanced for you, so don't worry about it.

  • Difference between a static class and singleton

    What is the difference between a static object and a singleton ?
    for example:
    1 )
    public class MyObject {
    static MyObject singleton ;
    public static MyObject getInstance() {
    if (singleton ==null) singleton = new MyObject ();
    return singleton;
    private MyObject () {
    supe();
    public void myMethod() {
    2 )
    public class MyObject {
    private MyObject () {
    supe();
    public static void myMethod() {
    If I need to call the myMethod() , witch of this solution is better ?
    // case 1
    MyObject.getInstance().myMethod()
    // case 2
    MyObject.myMethod()
    .....

    This has been discussed a lot here.
    Use the forum's search feature, or, since it's not necessarily reliable, use google.
    For many intents and purposes, they're equivalent.
    However, with a singleton you can implement an interface, and get polymorphic behavior. You can't do with with a class full of static methods (which, by the way, is NOT a static class).

  • Difference between ArrayList class and LinkedList class

    I would like to ask what is the difference between the ArrayList class and the LinkedList class? If I use ArrayList class can I have a fixed size?
    Thank you for your answers.

    I often use LinkedList when I need a FIFO. When all you're doing is adding to the end of a list and removing the first element, it is definitely faster. Here's a quick SSCCE I wrote up to demonstrate:import java.util.*;
    public class ListTest {
       public static void main(String[] args) {
          LinkedList<Integer> linked = new LinkedList<Integer>();
          ArrayList<Integer> array = new ArrayList<Integer>();
          Integer[] values = new Integer[2500000];
          for (int i = 0; i < values.length; i++) {
             values[i] = new Integer(i);
          Random rand = new Random();
          boolean[] randDir = new boolean[values.length];
          for (int i = 0; i < randDir.length; i++) {
             randDir[i] = rand.nextBoolean();
          long startTime, endTime;
          startTime = System.currentTimeMillis();
          for (int i = 0; i < values.length; i++) {
             if (linked.size() > 0 && randDir) {
    linked.removeFirst();
    else {
    linked.addLast(values[i]);
    endTime = System.currentTimeMillis();
    linked.clear();
    System.out.println("linked:"+(endTime-startTime));
    startTime = System.currentTimeMillis();
    for (int i = 0; i < values.length; i++) {
    if (array.size() > 0 && randDir[i]) {
    array.remove(0);
    else {
    array.add(values[i]);
    endTime = System.currentTimeMillis();
    System.out.println("array:"+(endTime-startTime));
    array.clear();

  • JSP - difference between extending class and importing class

    In the following scenario
    class A
    private static String con;
    Case 1
    ==========
    class B extends A
    Case 2
    ===========
    import A;
    class C
    A objA = new A;
    What will be the difference in Case 1 and Case 2 on accessing the variable con.
    thanx
    Venki

    >
    If i am not wrong, the above line should be private
    static variables are not inherited.
    NO, AFAIK, static variables, private or otherwise arent inherited. Please note that doesnt mean, its not available in the sub-class. Infact it is. However you cannot override or hide them ie polymorphism isnt applicable to static members which is what , IMO, inheritance is all about. Its logical, when you think about it, polymorphism is applicable for objects and static is a class thinggy, has nothing to do with objects.
    cheers,
    ram.

  • Practical differences between static IP and loopback installation

    Hi,
    I'm planning the installation of 10gR2 on my windows laptop at home. I can set my laptop up to use either a static or dynamic IP address before installing the db. I'm not sure which way would be best though. I'm aware that, if I use a dynamic IP/DHCP setup, then I will need to install the loopback adapter for Oracle to work properly.
    Are there any practical differences between a static IP db installation versus a dynamic one? I.e. if I choose the dynamic IP/loopback adapter approach, will I still be able to connect to my laptop's database via other pc's on my home network?
    My home setup looks like this.
    ISP -> DSL Modem -> Router -> various PC's
    I'd also like the flexibility to connect my laptop to my work's WAN and access my laptop db from my work PC. I suspect this will harder to do if I use the static IP approach.
    My laptop runs Windows XP and I'm planning to install either the Enterprise Edition or Personal Edition of 10gr2. I suspect that the Personal Edition, being restircted to a single user, may refuse connections from machines other than the one it is installed on, but I'm not sure about this. Can anyone confirm?
    Thanks in advance for your help.

    I personally prefer static IP too, what I can do with static and not dynamic is:
    use the IP address in various codes and scripts specially host and batch.
    you can reach the same result by using the hostname too BUT in case of windows that might be let's say not quite accurate all the time ;) specially when nslookup and DNS/WINS are drunk :))
    Tony Garabedian

  • Differences between Property Classes and Visual Attributes

    Hi folks
    Can anybody know the exact difference between a property class and a visual attribute?
    I'm substantially good at Forms but doesn't know the exact difference.
    Any reply will be deeply appreciated.
    Cheers,
    PCZ

    Visual Attributes are only for the visual representation of your items, buttons, blocks, canvas, ...
    Property Classes are for inheriting all objects including visual attributes.
    If you create a style guide for your company, then first define the visual attributes for the look and feel. After that create property classes for all of the other objects in your application. On top of that create object groups, where you centralize your property classes and visual attributes.
    If you have created such a form, then call this form reference-template. Now you inherit the object-groups to a second form and call this form "template".
    If you create new forms, copy the template and rename it. Then you have in all your forms inherited visual attributes, property classes, etc. and all this stuff is in one "reference-template"
    try it
    Gerd

  • Difference between abstract class and the normal class

    Hi...........
    can anyone tell me use of abstract class instead of normal class
    The main doubt for me is...
    1.why we are defining the abstract method in a abstract class and then implementing that in to the normal class.instead of that we can straight way create and implement the method in normal class right...../

    Class vs. interface
    Some say you should define all classes in terms of interfaces, but I think recommendation seems a bit extreme. I use interfaces when I see that something in my design will change frequently.
    For example, the Strategy pattern lets you swap new algorithms and processes into your program without altering the objects that use them. A media player might know how to play CDs, MP3s, and wav files. Of course, you don't want to hardcode those playback algorithms into the player; that will make it difficult to add a new format like AVI. Furthermore, your code will be littered with useless case statements. And to add insult to injury, you will need to update those case statements each time you add a new algorithm. All in all, this is not a very object-oriented way to program.
    With the Strategy pattern, you can simply encapsulate the algorithm behind an object. If you do that, you can provide new media plug-ins at any time. Let's call the plug-in class MediaStrategy. That object would have one method: playStream(Stream s). So to add a new algorithm, we simply extend our algorithm class. Now, when the program encounters the new media type, it simply delegates the playing of the stream to our media strategy. Of course, you'll need some plumbing to properly instantiate the algorithm strategies you will need.
    This is an excellent place to use an interface. We've used the Strategy pattern, which clearly indicates a place in the design that will change. Thus, you should define the strategy as an interface. You should generally favor interfaces over inheritance when you want an object to have a certain type; in this case, MediaStrategy. Relying on inheritance for type identity is dangerous; it locks you into a particular inheritance hierarchy. Java doesn't allow multiple inheritance, so you can't extend something that gives you a useful implementation or more type identity.
    Interface vs. abstract class
    Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent candidates inside of application frameworks.
    Abstract classes let you define some behaviors; they force your subclasses to provide others. For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract base class can declare abstract shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn't know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method. When the base class calls this method, Java calls the method defined by the child class.

  • Difference between abstact class and interface

    hi
    i am new in java.
    plz tell me abstract class support which type of polymorphisam and
    interface support which type of polymorphisam
    plz explain
    bye
    siva

    There's only one type of polymorphism.
    Does that make it mono-polymorphism? ;)
    Indeed - do a search for the forums.
    The short answer is that both abstract classes and interfaces define required method signatures for subclasses that extend/implement them, but abstract classes can have implementation for some or all methods that are not abstract; interfaces have no implementation whatsoever for any methods.

Maybe you are looking for

  • HP Pavilion DV6 Notebook -VX070AV, Windows 7 64-bit- no sound

    HP Pavilion DV6 Notebook -VX070AV, Windows 7 64-bit. IDT High Definition Audio CODEC Enabled stwrt64.sys 6.10.6289.0 Intel (R) Display Audio Enabled IntcDAud.sys 6.12.0.3047 Speakers show sound coming from internal speakers. Speakers and Headphones P

  • Adding field to standard screen in tr. FPSEC1

    Hi, I need to add customer field to standard screen in FPSEC1 transaction (screen 202,second screen in transaction (non cash choice)). Has someone did it already? (please don't give me general advices as "find screen exit" and so on...) Point's will

  • How to delete the spreadsheet and chart icons

    iam having a view called "calender":in which the html page is having this code: <%@page language="abap" %> <%@extension name="htmlb" prefix="htmlb" %> <%@extension name="thtmlb" prefix="thtmlb" %> <%@extension name="xhtmlb" prefix="xhtmlb" %> <%@exte

  • Display colors suddenly off

    Just added RAM and a new external harddrive to my mac and now the color on my display is all out of whack. Whites have a red tinge, reflex blues are cyan, and greens are desaturated. Recalibrated and tried adjusting color settings on the monitor itse

  • Change my email in Creative Cloud

    I can't change my email in the creative cloud!! I downloaded the creative cloud lately but I can not log into it because he's saved with my old email. How do I change the email? Please help me!