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]
# 1

> 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

kajbja at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 2

incidentally, this statement "I know that the modifier static defines that the method will exist once in the memory" isn't quite right. the static modifier doesn't do this, every method exists only once in memory, whether it's static or not. java objects don't carry around copies of method code, they all share the same one

georgemca at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 3
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?
juanitaJa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 4

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?

juanitaJa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 5

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

esaliyaa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 6
Can you please define the "If some functionality does not depend on the object " and give me an example?Can a static method have as an argument a bean object like User? p.s. Yes when I mean static class I mean a class with sttaic methods.
juanitaJa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 7

>"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.

kalania at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 8
ok and what about this?Can a static method have as an argument a bean object like User?....thanks..
juanitaJa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 9
> ok and what about this?> > Can a static method have as an argument a bean object> like User?> > ....thanks..why wouldn't that be allowed? it can, btw
georgemca at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 10
so why an Action in Struts could not be static? I mean have static methods?
juanitaJa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 11
> so why an Action in Struts could not be static? I> mean have static methods?why would you want it to?
georgemca at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 12
Why not to?I know it is crazy :) but I am just trying to understand the static concept and based on what we discussed Action is not depending on anything in the class.Can you please give me an example of "then it does not depend any other stuff of that class."?..thanks..
juanitaJa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...
# 13

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.

esaliyaa at 2007-7-8 21:54:18 > top of Java-index,Java Essentials,New To Java...