简体   繁体   中英

Context Parameters vs. public static final Variables in Java Web App

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.

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM