Continuation with my first Java project , Today I've started my second Java routine which will read Lotus Notes documents from other database and will push to Oracle.I've started with first class which handles one of the form ( still 16 forms to go :) ).Once code is written and tested , I've started reviewing my code which I always do to check if any logical or memory consumption stuffs are poorly coded.
While doing I got confused with public and static declaration . Initially I've done non static declaration of all JDBC objects;e.g,Connection, Statement and RecordSet on class level.Even I was reading "Java 2. The complete reference" book side by side but not able to find if I could get any performance benefit in for "static" declarations.So I've decided to test on execution time basis , don't know if it's the best way.Here is what I've done;

I've added "ExecutionTimer" class in my code,


public class ExecutionTimer {
private long start;
private long end;

public ExecutionTimer() {
reset();
}

public void start() {
start = System.currentTimeMillis();
}

public void end() {
end = System.currentTimeMillis();
}

public long duration(){
return (end-start);
}

public void reset() {
start = 0;
end = 0;
}

}


And I've used like



ExecutionTimer t = new ExecutionTimer();
t.start();

ProcessTable objProcesTable = new ProcessTable(session);
objProcesTable.initiateObjects(session);

t.end();
System.out.println("\n" + t.duration() + " ms");

Here in my class skeleton ,


public class ProcessTable {

static private Connection conn; // or private Connection conn;
static private Statement stmt; // or private Statement stmt;
static private ResultSet rs; // or private ResultSet rs;

public void initiateObjects(){

//Initiate all domino objects


insert() //Insert record
update() //Update record
delete() //delete record
}

ProcessTable(){
// Prepare connectivity with Oracle
}

public void insert(){
//insert logic
}

public void update(){
//update logic
}

public void delete(){
//delete logic
}

}


I've done some testing in my live environment with 10 records and found declaring these variables as static is faster than declaring public.I've tested thrice to make sure if network operation may create the difference. Here are results,

First test : ( In mills)
--------------------------------

Using non-static: 23791 ms

Using Static : 23134 ms

Second test:
--------------------------------

Using non-static : 23258 ms

Using Static : 21930 ms

Third test:
--------------------------------

Using non-static : 24556 ms

Using Static : 23227 ms

Here is what I've found in Java book ,


Instance variables declared as static are, essentially, global variables. When objects
of its class are declared, no copy of a static variable is made. Instead, all instances of the
class share the same static variable.
Methods declared as static have several restrictions:
■ They can only call other static methods.
■ They must only access static data.
■ They cannot refer to this or super in any way. (The keyword super relates to
inheritance and is described in the next chapter.)


None of the above restrictions are applied in my case, so declaring static is faster approach ??? hope someone can enlighten on this.

Leave a Reply

preload preload preload