Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Using static variables and methods in java classes

Using static variables and methods in java classes

September 23rd, 2009 Vinay Leave a comment Go to comments

In one of the re factoring exercises done on our old code bases , we had a discussion as to why we should switch a reference to static variable with a static factory method.
Here I will explain you with code what I am talking about .

Below is code of  Constants class which was being used  by other classess to get DateFormat object

public final class Constants {

// other objects and variables

public static final DateFormat STD_DATE_FORMAT = new SimpleDateFormat(“MM/dd/yyyy”);

}

The class which was using this was

public ClassABC {

public void doSomething(){

//some logic
Constants.STD_DATE_FORMAT.format(someObejct.getDate()));

}

}

Constants class is replaced by DateFactory Class

public final class DateFactory {

public static final String HH_MM = “HH:mm”;

public static DateFormat getDateFormatInstance_HH_MM() {
return new SimpleDateFormat(HH_MM);
}

// some other methods

}

Now the discussion was how this approach of creating a new object is a good practice and it can avoid concurrency issues. This would be tested by the code below

Here is class UsesConstant which has two methods

Here is class UsesDateFormat that has two methods.

public class UsesDateFormat {

public DateFormat helloUsesDateFormat(){

Date temDate = new Date();
DateFormat dif3 = DateFactory.getDateFormatInstance_HH_MM();

return dif3;

}

public DateFormat helloUsesConstant(){

Date temDate = new Date();
DateFormat dff = Constants.STD_DATE_FORMAT;
String dateFormatted = dff.format(temDate);
return dff;

}

public class UsesConstant {

public DateFormat helloUsesConstant(){

Date temDate = new Date();
DateFormat dff = Constants.STD_DATE_FORMAT;
return dff;

}

public DateFormat helloUsesDateFormat(){

DateFormat dif = DateFactory.getDateFormatInstance_HH_MM();
return dif;
}

Look at the TestClass main method which instantiates these two classes and calls the method. The two classes above simulate the real word classes (service , DAO or Action )

public static void main(String[] args) {

UsesConstant usesConstant = new UsesConstant();
UsesDateFormat usesDateFormat = new UsesDateFormat();

DateFormat difConstant1 = usesConstant.helloUsesConstant();
DateFormat difConstant2 = usesDateFormat.helloUsesConstant();

System.out.println(“(difConstant1 == difConstant2) ” +(difConstant1 == difConstant2) +” difConstant1.equals(difConstat2) ” + difConstant1.equals(difConstant2)+ ” difConstant1: ” + difConstant1+  ” difConstant2: ” + difConstant2);

DateFormat dif1 = usesConstant.helloUsesDateFormat();
DateFormat dif2 = usesDateFormat.helloUsesDateFormat();

System.out.println(“(dif1 == dif2) ” +(dif1 == dif2) +” dif1.equals(dif2) ” + dif1.equals(dif2)+ ” dif1: ” + dif1 +  ” dif2: ” + dif2);

}

And the out put was

(difConstant1 == difConstant2) true difConstant1.equals(difConstant2) true difConstant1: java.text.SimpleDateFormat@7c669100 difConstant2: java.text.SimpleDateFormat@7c669100

(dif1 == dif2) false dif1.equals(dif2) true dif1: java.text.SimpleDateFormat@4183e5a dif2: java.text.SimpleDateFormat@4183e5a

difConstant1 == difConstant2  return true because they are the same object returned from a static variable from Constants class

(dif1 == dif2) return false because they a new object has been created each time a request is made.

So why does dif1.equals(dif2) return true. .equals() checks for deep value and since they are blank objects , there value is same.

All right so why is the address of the DateFormat objects same even when they are instantiated as new objects ?

I think  jvm internally optimizes how the obejcts are created and represented in memory space .

Any other thoughts ?

Look at the class TestClass which instantiates these two classes and calls the method. The two classes above simulate the real word classes (service , DAO or Action )

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
  1. No comments yet.
  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes