No Global Variables In Java ?
Hello,
I understand that Java does not implement Global Variables (or so it's said) - however, isn't a static variable just the same thing? What are the differences practically?
With a static variable I can access it from other class files and manipulate it... so there "scope" of the static variable is quite broad... Using a static variable in this way may be poor design, as opposed to using a setter/getter methods to get a local variable from another class... however it is still possible to just grab the static variable.
Code:
public class Test1 {
static String str1 = "I am a String!";
}
Code:
public class Test2 {
public static void main(String[] args) {
System.out.println(Test1.str1);
}
}
This code would print the static string from the Test1 class file... how is this not regarded as a "Global Variable"?
Re: No Global Variables In Java ?
What you have is a good example of how to implement global variables in Java, yes. However, should you use it? I'd rather not.
I find this rather interesting, and there are many other sites that can help you understand: Global Variables Are Bad
Re: No Global Variables In Java ?
Interesting article Zyril and good find! I do understand the use of Global Variables can be bad (with exceptions)... however I've been taught that Java explicitly does not make use of Global Variables, as in they simply are not in the language by design. (I think this is true for most OOP languages?) -- but the question that I have is, if this is true, then whats the difference between a global variable in say, C, as apposed to a static variable in Java? It seems to be a static variable IS a global variable, with a fancier name...
Re: No Global Variables In Java ?
What's a global variable? (Genuine question as I don't have any CS education and am too lazy to google...) The idea of a variable being static is defined as part of the Java language ("global" isn't: that's not to say that it's meaningless, just that the term isn't precisely defined as part of the Java language, presumably because it isn't implemented.) A couple of things occur to me:
Static variables have a type. So when you access a static variable it's not merely a matter of get-a-value/set-a-value, the class itself will be loaded which may have all sorts of side effects. (The semantic aspects of the process are discussed in more detail than you really want at JLS 12.4)
The class and the variable are subject to access modifiers which will affect from which sections of code they can be accessed.
---
The fact that static variables (like static methods) are linked to a class is often reflected in the fact that, in a Java context, they are often referred to as static (or class) fields.
Re: No Global Variables In Java ?
The concept of global variables in Java doesn't exist, but you can of course create similar scenarios to as what other programming languages would refer to as global variables.
The use of static in Java in front of a variable is to make it a Class variable. They are referenced with the class name, and the variable belongs to the class and not the object (instance). There will only be one copy of the class variable when the class is loaded into the Java Virtual Machine. If one object instance of that class changes the value of it, then all the other instances see the same changed value. I bet you see the use of this, and at the same time understand why it is considered "evil". Remember that all instances of the class can modify it?
As to your original question
Quote:
then whats the difference between a global variable in say, C, as apposed to a static variable in Java?
I'm perhaps out on thin ice here but as far as I can understand, there is no difference.
Hope I could give you any useful information!
Re: No Global Variables In Java ?
A Global Variable is a variable that is accessible in every scope... meaning it can be used in any method/function within a complete program... The guys who developed Java (James Gosling + team) explicitly designed it to not include the use of Global Variables... since they typically are not used correctly or may lead to other issues later on (as described in the article linked by Zyril).
However, it appears you can still achieve a Global Variable by using static...
perhaps the difference is how the variable is initialized? --> A global gets initialized at the start of the program whereas a static can be declared but initialized later on (possibly from a different class entirely)?
Re: No Global Variables In Java ?
Zyril -- i think your explanation does shed some light on this for me -- I was typing my follow-up as you posted your response!
So in essence (and practicality), a static IS global -- including all the pitfalls of a global as well (such as synchronization of the static variable if multiple parts of a threaded program touch it, etc). I've found static variable to be helpful in the past, but I can also see where they would cause issues and would be best implemented in a setter/getter ...
I guess the Java textbooks would be better written to explain the similarities of the Global/Static variables instead of simply saying Java doesn't have Global variables... period...
Re: No Global Variables In Java ?
Quote:
Originally Posted by
SnakeDoc
Zyril -- i think your explanation does shed some light on this for me -- I was typing my follow-up as you posted your response!
So in essence (and practicality), a static IS global -- including all the pitfalls of a global as well (such as synchronization of the static variable if multiple parts of a threaded program touch it, etc). I've found static variable to be helpful in the past, but I can also see where they would cause issues and would be best implemented in a setter/getter ...
I guess the Java textbooks would be better written to explain the similarities of the Global/Static variables instead of simply saying Java doesn't have Global variables... period...
The key point (which you touch on in your previous post, an pbrockway mentions) is that a static variable cannot exist without the class of which it is a part.
It does not exist until someone attempts to use the class of which it is a part. When the class is loaded then the static is declared and initialised.
It is this simple fact, that a static variable in Java has the scope of the class of which it is a part, that means it is not a global variable.
It may act like one in some cases, but it isn't one.
Re: No Global Variables In Java ?
Thanks for correcting me! I always learn something new here!
Re: No Global Variables In Java ?
I think Tolls just laid it down lol!
Thanks Tolls for the clarification - it added valuable knowledge to what Zyril was able to provide already -- Thanks everyone! Good answers!