Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] static variables within a method?

Started by arsenius Apr 17, 2002 at 10:34 PM 5 replies 1.1k views
Original Post
arsenius
arsenius
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
mjacobsca
mjacobsca
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
Tac-Tics
Tac-Tics

  
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).
Knudde
Knudde
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
Idej-Avaj
Idej-Avaj

    //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]
Tac-Tics
Tac-Tics
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 =-(

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.