Results 1 to 3 of 3
  1. #1
    felixtfelix is offline Member
    Join Date
    Dec 2007
    Location
    Mumbai, India
    Posts
    37
    Rep Power
    0

    Default static constant and performance

    Hi,

    We are having a web application. being a web application everyone knows that it is multithreaded. For each and every request n number of string constants will be created. whether is it a right way to put all those string constants in a class and access it in the other classes. For example, I have a class named FieldConstants which will have n number of constant variable(possibly around 5000)

    public Class FieldConstants{
    public static final String EMP_NAME = "Employe Name";
    public static final String EMP_CODE = "Employe Code";
    .
    .
    .
    }

    public class EmployeeDetails{
    public List getEmployee(){
    ResultSet rs ;
    List employeeList = new ArrayList();
    .
    .
    rs = pstmt.executeQuery();
    while(rs.next()){
    employeeList.add(FieldNameConstants.EMP_NAME);
    }
    return employeeList;
    }

    This is just a sample. Generally using this concept, nothing will be directly hard coded in the code rather it will come from the class FieldConstants class in the entire application.

    Whether will it boost the performance if yes then how? There is no doubt that it will be a big problem when we go for debugging the code. One of my collegue suggests this idea. Your valuable suggestions are required.

    Regards
    Felix

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by felixtfelix View Post
    This is just a sample. Generally using this concept, nothing will be directly hard coded in the code rather it will come from the class FieldConstants class in the entire application.
    That is still hard-coding, however. You also know that public static final variable values are compiled directly into the classes using them, right? So a change to one of those variables still means that you have to recompile and redploy your entire application.

    Sounds more like something that you should be making a ResourceBundle for so that it can be easily "internationalised".

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    It will have no effect on performance.

    This:
    Java Code:
    employeeList.add(FieldNameConstants.EMP_NAME);
    will take up exactly the same space as this:
    Java Code:
    // Class static -
    provate static final String EMP_NAME = "Employe Name";
    
    ...
    employeeList.add(EMP_NAME);
    ...
    Actually, it will probably take up a tad more...

    Constants for display on the front end should be held as some form of resource (see above post). Constants representing columns in a table (if you're using JDBC and not Hibernate or similar) should be held in whatever data access class is most relevant, since that'll be the only place they're needed.

Similar Threads

  1. Replies: 2
    Last Post: 06-08-2010, 02:21 PM
  2. Replies: 4
    Last Post: 05-12-2010, 12:05 PM
  3. Constant size for JTextArea
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2008, 05:12 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •