DriverManager in JDBC

A

asit

In java, we can't instantiate DriverManager class becoz the
constructor is private.

Why this is so ???

Why the constructor is provided ???
 
L

Lew

asit said:
In java [sic], we can't instantiate DriverManager class becoz [sic] the
constructor is private.

Why this is so ??? [sic]

Why the constructor is provided ??? [sic]

You only need one question mark at the end of a sentence to indicate
an interrogative in English.

See Joshua Bloch's excellent book /Effective Java/, Item 1, "Consider
static factory methods instead of constructors" for a detailed
explanation of this idiom.

The short version is that 'java.sql.DriverManager' is a class that you
should never instantiate. It is a utility class that has no state of
its own and all its methods are static (class level). It makes no
sense ever to have an instance of the class, so the API designer
ensured that no client can instantiate the class.

It also prevents specious inheritance of the class. ("Design and
document for inheritance or else prohibit it", /ibid./) It makes as
little sense to inherit from a class of nothing but static methods as
it does to instantiate it.

Some of the methods in 'java.sql.DriverManager' are factory methods
that return implementation instances by their interface type. By
keeping the actual concrete type hidden, the factory has the
flexibility to provide any implementing type, even a different one
from time to time, without damage to client code. This is part of
what is sometimes called, "programming to the interface" or
"programming to the type", a best practice of defining behaviors at
the interface level and generally hiding the concrete classes
involved.
 
A

Arne Vajhøj

asit said:
In java, we can't instantiate DriverManager class becoz the
constructor is private.

Why this is so ???

Why the constructor is provided ???

To prevent you from construction an object that you
should not construct.

Arne
 
R

Roedy Green

In java, we can't instantiate DriverManager class becoz the
constructor is private.

Why this is so ???

because you are supposed to instantiate it with a factory.

See http://mindprod.com/jgloss/factory.html

see http://mindprod.com/jgloss/jdbc.html for sample code to use
DriverManager to get a connection.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 
A

Arne Vajhøj

Roedy said:
because you are supposed to instantiate it with a factory.

No. You are not supposed to instantiate it at all. Not with
constructor and not with factory.

DriverManager is a factory for Connection, but that is
something else.

Arne
 
R

Roedy Green

I don't think you are supposed to instantiate DriverManager at all. None
of its public methods returns a DriverManager

Right. Its getConnection factory returns a Connection. DriverManager
has nothing but static methods. Its private constructor is to stop
you from trying the instantiate it.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top