aspose file tools*
The moose likes Java in General and the fly likes Single tone Vs Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Single tone Vs Static " Watch "Single tone Vs Static " New topic
Author

Single tone Vs Static

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
Hi every body I have one dought ,if I am wrong please clarify me :

We know that singletone is basically create one object through the application , if I create one static object instead of singletone objetc what will happen ? Is the singletome object and one static object same ?

thanks ans regards,
S
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
s begri wrote:
if I create one static object instead of singletone objetc what will happen ? Is the singletome object and one static object same ?


A Singleton is a class that's instantiated once only and can be accessed globally. A Singleton is a design pattern and it has several possible implementations in Java. The most popular right now seems to be based on an enum.

But there's no such thing as a static object. Do you mean a class with only static members that's never instantiated? Well, that will be a proper Singleton for many purposes.
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
uj nossnahoj wrote:
A Singleton is a class that's instantiated once only and can be accessed globally. A Singleton is a design pattern and it has several possible implementations in Java. The most popular right now seems to be based on an enum.


Can you give one example for enum so that I can understand better ,
regards,
S

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
But there's no such thing as a static object. Do you mean a class with only static members that's never instantiated? Well, that will be a proper Singleton for many purposes.


Let's say I have only one Connection object & I want to use it my app , & after doing all jobs I want to close the connection .In this case I can impliment The Connection class as a single tone , or I can crete one static object of type connection ,

Whis one is better and why ?

regards,
S
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
s begri wrote:In this case I can impliment The Connection class as a single tone , or I can crete one static object of type connection ,


It's called a Singleton.

And what do you mean by static object?
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
Thanks for your reply ,
lets say :



Here I have created one static Connection reference , is it good ? or should I go for single tone .

regards,
S

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
That is a static reference, not a "static object".
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
Yes,
but which one is good ? Making connection by this way or by using single tone ?

regards,
S
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
s begri wrote:Here I have created one static Connection reference , is it good ? or should I go for single tone .


The static conn variable is private and thus part of the implementation of some class.

The main use of a Singleton is when you want to provide global access, so in this case I don't think you should use one. Keep conn private.

And please start using the correct term: Singleton. A single tone is something completely different.
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
Thaks for correct me ,
if I make the Connection as a public such as

public static Connection conn , then ?



regards,
S
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
No, don't make fields public. It would allow other code to modify your Connection if you did that.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

s begri wrote:Thanks for your reply ,
lets say :



Here I have created one static Connection reference , is it good ? or should I go for single tone .

regards,
S



Every call to getConnection() would return a new instance of Connection. If I call getConnection() twice I would have two open connections, and the conn static variable will only refer to the second one. Unless you maintain the reference to the Connection returned by getConnection() throughout your work you would not be able to re-acquire a reference to the same Connection (at least not safely) that you originally got from the getConnection() method. So the static field conn is useless, really, since it can not be relied on to give you the connection you want.

I don't see any relationship in the code you provided and the Singleton Design Pattern. Perhaps some research on what a Singleton is will help?


Steve
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 245
Thanks for you guys replies ,

see the code actually little bit different :




I think this is preatty much cool .


regards,
S
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
s begri wrote:
if I make the Connection as a public such as

public static Connection conn , then ?


That changes things. Now conn is a "global" variable which can be reached from everywhere in your program. In that case the preferred OO implementation is to use a Singleton to represent the "connection".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
No, it is worse than that. Every time anybody tries to get a Connection, a new Connection is created.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Campbell Ritchie wrote:No, it is worse than that. Every time anybody tries to get a Connection, a new Connection is created.


What is worse than what and who are you talking to?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
No, I think I have managed to miss out the latest post. Sorry. My mistake. If you miss out the == null test, then you get the problem of multiple Connections. If you correct it by adding the == null test, there is a slight risk of getting two Connections if the method is not synchronized, but otherwise you have sorted out the problem.
Sorry again.
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125


Basically, this is a very bad design. A few problems:

Never makes Database Connections Static or Singleton. Why?

Say two threads T1 & T2 calls the getConnection method. Based on your design, both the threads are now having the same connection object. t1 called the
connection.close()
, and t2 tries to do something with the connection, it gets and exception there as the connection is already closed.

The solution:

Leave the getConnection method as it is (non-Static) and don't keep a static connection object as a Class Member. Use a Connection Pool in you Tomcat/Application server, and it will take care of everything.

Software coders always have a tendency to unnecessarily use patterns (They recently heard) and try to implement in the wrong place. That tends to become an
Anti-Pattern


Cheers
Aneesh


Cheers
Aneesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Single tone Vs Static