File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Right usage of static modifier ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Right usage of static modifier ?" Watch "Right usage of static modifier ?" New topic

Right usage of static modifier ?

Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
In a application,in which every method need to be connected with database,i made a separate class whose constructor has procedure to connect with database,I defined all the variables
static so that all the methods in caller class refer to same variable,
is this a right usage of static modifier?Code for both classes is given below.
import java.sql.*;
class dbconn {

static Connection c;
static String dbUrl = "jdbc dbc b1";
static String user = "";
static String password = "";

dbconn() {

try {
c = DriverManager.getConnection(dbUrl, user, password);
catch(ClassNotFoundException n) { }
catch(SQLException s) {}
class empinfo {

void empadd() {
new dbconn();
void empdel() {
new dbconn();
... }

public static void main(String args[]) {

empinfo e = new empinfo();

Rob Ross

Joined: Jan 07, 2002
Posts: 2205
It's hard to say if keeping your connection info static is the right solution for your problem, but if you are going to do it this way, you should use a static initializer to set up your connection, or at least use static methods.
The way you have it written now, you are saving the connection info in static variables, but you are re-initializing them every time you construct a new dbconn object. In the constructor, you re-establish the connection every time. You might just want to check first if c != null, and if so, don't re-connect.

SCJP 1.4
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
For your application the Singleton design pattern seems to be a good fit. In your example you want a single database connection so why not have a single instance that you use for database access?
If you have to many users you'll have to look into pooling multiple connections to the database, but that's a little complex.

This will prevent you from having Connections made willing nilly all over the place. Add a little logic to track the number of active connections and your half way to a dB pooling algorithm.
[ March 09, 2002: Message edited by: Michael Pearson ]
Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
Thanks a lot for your help,as far as I understand that what i'm doing wrong is instantiating dbconn in each method,but as i've
read static variables are not disturbed by instances but they continue to be used by objects on one place where they were first created
so why when i instantiate dbconn they r agian reinitialized?
Please make me clear where i'm taking wrong this static keyword.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Well, I think that you understanding of static variables is a bit fuzzy.
Static variables are not associated with any particular instance of the class. Therefore they are not kept in the area allotted to track the state of any particular instance.
Instead the static variables are associated with the class that they belong to. That means that they are located in the central classfile for the class. Each and every instance of the class can get at the central static variable equally easily, and all of them can update the static variables equally easily with the updates being visible to each and every instance of the class.
If each instance updates the static variable then they are overlaying or modifying the updates done by the previous instance. As you can see, this could cause problems if not controlled properly.
I agree. Here's the link:
subject: Right usage of static modifier ?
jQuery in Action, 3rd edition