question about static
It might sounds stupid what I will ask but I need to understand some basic concepts of Java.
Lets say we have a class with static methods.
I know that the modifier static defines that the method will exist once in the memory and it is independent from the instances of the class that might be created.
So lets say that there are two users accessing an application.This application has a class LocaleUtil with a method that retrieves
the correct properties from a messages.properties file based on the Locale defined in an appropriate xml.If one user press english language and the property is loaded from the
messages_en.properties and the other user presses french language then the method in this application will return messages_fr correct?
This is my code in LocaleUtil
publicstatic String getLocalizedValue(FacesContext context, String key){
String messageBundle = context.getApplication().getMessageBundle();
Locale locale = context.getViewRoot().
getLocale();
ResourceBundle resourceBundle = ResourceBundle.
getBundle(messageBundle,
locale);
String value = resourceBundle.getString(key);
return value;
}
I guess static means that a class will be instantiated once not matter how many users use the application?But it will return different values for different parameters?
Or it is instantiated once in every session ?
As you can understand I am a little bit confused.
[1652 byte] By [
juanitaJa] at [2007-11-26 15:36:35]

> I guess static means that a class will be
> instantiated once not matter how many users use the
> application?
No, You are probably thinking about static initializers, and not static methods.
> But it will return different values for
> different parameters?
Correct.
Kaj
ok..thank you for your answers.
So I guess a static class will be instantiated once not matter how many users use the application correct?
Or it will be instantiated once per every user?
In struts for example why we don't make static every action since it will
return different forwards for different parameters?
Which classes we must define as static? (I mean their methods)?
For example a class with Constants it must be one of them correct?
I'm not sure about what you mean by "static classes" in Java. Anyway you can define static methods. Those will not go into objects instantiated using that class. They will be there in the class level simply so that any other method can invoke the same copy. The main advantage is that you can get rid of redundant code. A nice example is the java.lang.Math class.
If some functionality does not depend on the object on which it's invoked and also if it is a common function among several objects then it's better to make it static. Constants defined in a class will be static (note: not the class but the constants are static)
I hope this will help you out with your problem
>"If some functionality does not depend on the object " and give me an example?
ok this is a very simple example.
If you want to return square root of a number by just taking in the number we give and calculating the square root,then it does not depend any other stuff of that class.
Then you can make that method static.
Ok to get the concept of the static take java.lang.Math class. You can invoke methods like Math.sin(), Math.abs() without creating an object out of Math class. That means those methods are static.
To make it more clearer think of a method sqrt() which will return the square root of a given number. You can either define this as public double sqrt(double a) or as public static double sqrt(double a). If you do it the first way then each object of your class will get a sqrt method and if you do it the latter way then you can call that method without creating objects. In fact the latter way is better than the first for this method.
So why make it static? The sqrt method performs the same task. That functionality does not have consider any of the properties of the object. Thus it does not depend on the state of the object. Then it's better to make it static to avoid redundant code.