Archived

This topic is now archived and is closed to further replies.

[java] static variables within a method?

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

if you''ve programmed C++ i''m sure this will make perfect sense to you. I want to define something static, as it would have been in C++, where everytime you reenter a function/method, it will have the same value as when you left. I know i can define it so as to just be a member of the class, but i was planning on using a number of these, and that would make for rather messy code outside of the method. any suggestions? what i mean is this (this should work in C, not totally sure, haven''t used it in a while) void blah() { . static int I=5; . i++; } everytime we come back into this function, I will be 6, 7, 8... 5 will only be assigned when the variable is declared. Is there a way to do this with java? -arsenius ''after three days without programming, life becomes meaningless'' -The Tao of Programming

Share this post


Link to post
Share on other sites
You can''t do that in Java. Variables are only scoped for the method they are declared in, and go out of scope as soon as the method ends. You can''t declare them static like C++. This is somewhat of a limitation of Java.

If you plan on having a number of variables you need access to during the lifetime of the class, you don''t need to make them all static. For instance, you can create an inner class, make THAT object static, and put a lot of your variables inside this inner class. That way, you have 1 static variable containing the inner class object, and you don''t have lots of little static variables clogging the class definition. This might even be a more object oriented way of doing things. I''m sure that this was probably some of the rationale behind the design decision in the first place.

Michael

Share this post


Link to post
Share on other sites

  
class Foo
{
private int l;
public void blah()
{
l = 5;
l++;
}

/*
Useful code =-)
*/


}



In Java, you just make what would be static local variables into static class variables. It produces essentially the same results, but in addition, all other methods may access the variable (which may or may not be useful, depending on the situation, but it''s possible).

Share this post


Link to post
Share on other sites
You can also use static variables in java. The static variable will be the same variable to all instances of the class. If you want your variable independent for each instance, but storing the value independent of the function class, you need a class variable. You declare it inside the class but outside any method.

ciao knudde

Share this post


Link to post
Share on other sites

    //Number.java

public class Number{
public static int myInt = 2;
}


//MyApp.java

public class MyApp{
public static void Main(String [] args){
// prints "2" to the console

System.out.println(Number.myInt);
}
}


an example

[edited by - idej-avaj on April 18, 2002 10:27:38 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by arsenius
thanks folks, that''s pretty much what i thought the situation was. i''ve become so disinchanted with java lately because of such things *sigh*



I find C++ intolerable to use for similar reasons. Whenever I sit down to write something the way I always do it in Java, I''m always annoyed C++ doesn''t support it and I have to go do it the ugly and unfamiliar way =-(

Share this post


Link to post
Share on other sites