Static Variable

Hi

I have one package with one method and I want to refer to that method like this:

myPackage.myClass.myMethod();

For this I have to declare my method as static. My problem is if I declare the method static I will have just one instance of that method in the page. If I pass a String to the method and then return the String with modification, is possible that the returned String was overwritten with another String that other client pass at the same time?

Thanks

Lu韘 Matos

[511 byte] By [lghm@aeiou.pta] at [2007-10-2 9:20:39]
# 1
No. Each method invocation get its own private copies of local variables.
RadcliffePikea at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 2
Hi RadcliffePikeThanks for the reply. In this case what is the diference between the static method or non static?ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 3

The difference between static and non-static applies only to class/instance variables, not local variables:

public class Foo {

private static int classVariable;

private int instanceVariable;

public static void classMethod() {

classVariable = ... // This is shared between all invocations of classMethod()

int localVariable = ...// Each time this method is invoked, a private instance of this variable is created.

// instanceVariable... Can't access the instanceVariable from a static method!

}

public void instanceMethod() {

classVariable = ... // This is the same variable used in the classMethod.

int localVariable = ... // Again, a private instance to this method.

instanceVariable = ...// Each instance of this class (i.e. an object) has its own private instance of this variable.

}

}

Hope this example illustrates some of the differences.

RadcliffePikea at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 4

Hi RadcliffePike

Thanks again for the reply.

Just one question:

In the following example I will don't have any problem if the method myMethod was used by two clients at the same time? Just the variable myVar is the same to all the requests of the method?

In this case should the class myClass be static to? Why not? What's the diference?

public class myClass {

private String myVar;

public static String myMethod(String var) throws Exception {

Connection CONN = null;

PreparedStatement st = null;

ResultSet rs = null;

st = CONN .prepareStatement("select something from sometable");

rs = st.executeQuery();

String data = rs.getString("data");

CONN.close();

return(data);

}

}

Thanks

Lu韘 Matos

lghm@aeiou.pta at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 5

> In the following example I will don't have any

> problem if the method myMethod was used by two

> clients at the same time?

Well, myMethod() does not use myVar in your example, so myMethod is safe to call by as many threads as you want. If myMethod did use myVar, then you would want to think about synchronizing myMethod.

> Just the variable myVar is

> the same to all the requests of the method?

The variable myVar is not the same to all invocations of myClass's methods. It is only the same for invocations made on the same object. If myVar were static, then it would be shared accross all invocations.

> In this case should the class myClass be static to?

> Why not? What's the diference?

First, there's no such thing as a static top-level class. I can't pinpoint the source of confusion here, but it seems like you may be confusing variable scope and synchronization. The Java tutorial is probably the best resource I could point you to. Many, if not most, Java developers have probably got started with Java by going through the tutorial:

Here's the part that talks about classes and variables:

http://java.sun.com/docs/books/tutorial/java/index.html

And another section on threading:

http://java.sun.com/docs/books/tutorial/essential/threads/index.html

RadcliffePikea at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 6
> No. Each method invocation get its own private copies> of local variables.Do you mean to say that each call to a static method would create a new instance of the class object would be created?
kilyasa at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 7

> > No. Each method invocation get its own private

> copies

> > of local variables.

>

> Do you mean to say that each call to a static method

> would create a new instance of the class object would

> be created?

No, no. I'm talking about local variables declared in a method.

RadcliffePikea at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...
# 8
Hi RadcliffePikeThanks for your help. I will take a look at the links.ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 23:27:41 > top of Java-index,Other Topics,Algorithms...