0

I am developing a Java web application. I was wondering what the advantages/disadvantages are of setting a context parameter vs just declaring a public final static variable of a class in my web application.

For example, I have a variable that is constant throughout my application such as "serviceFee". I can store this variable as a context parameter (which will mean that I can only access it when I have access to the ServletContext Object)

or

I can set the value as a public static final variable in one of my classes (eg: my Invoice class) (and then I'll be able to access it all of my classes without having access to the ServletContext Object).

I'm looking for any advice on why one method is preferred over the other.

Thank you.

theyuv
  • 1,556
  • 4
  • 26
  • 55

1 Answers1

1

public final static variables should not be stored in the context, in the context you'll most likely store all the variabels that are used for whoever connects to your web app. I have always used a different interface class to store my variabels ex :

public interface constants {
    String foo = "bar";
}
namlik
  • 182
  • 1
  • 12
  • Thanks. How do you decide what should be a context variable and what should be a public final static variable? Why use one over the other? – theyuv May 03 '16 at 07:43
  • The biggest difference is, is it going to change? If yes look towards context if no look towards public final static int. This might give you more insight: http://www.ibm.com/developerworks/websphere/techjournal/1301_stephen/1301_stephen.html – namlik May 03 '16 at 07:54