Results 1 to 10 of 10
Thread: No Global Variables In Java ?
- 02-07-2013, 06:52 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 129
- Rep Power
- 0
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.
Java Code:public class Test1 { static String str1 = "I am a String!"; }This code would print the static string from the Test1 class file... how is this not regarded as a "Global Variable"?Java Code:public class Test2 { public static void main(String[] args) { System.out.println(Test1.str1); } }
- 02-07-2013, 08:25 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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
- 02-07-2013, 08:38 PM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 129
- Rep Power
- 0
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...
- 02-07-2013, 09:00 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
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.
- 02-07-2013, 09:04 PM #5
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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
I'm perhaps out on thin ice here but as far as I can understand, there is no difference.then whats the difference between a global variable in say, C, as apposed to a static variable in Java?
Hope I could give you any useful information!
- 02-07-2013, 09:21 PM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 129
- Rep Power
- 0
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)?
- 02-07-2013, 09:27 PM #7
Senior Member
- Join Date
- Apr 2012
- Posts
- 129
- Rep Power
- 0
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...
- 02-08-2013, 10:07 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: No Global Variables In Java ?
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.Please do not ask for code as refusal often offends.
- 02-08-2013, 03:59 PM #9
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: No Global Variables In Java ?
Thanks for correcting me! I always learn something new here!
- 02-08-2013, 05:43 PM #10
Senior Member
- Join Date
- Apr 2012
- Posts
- 129
- Rep Power
- 0
Similar Threads
-
Variable Storage and Global Variables in Java
By lsrinivasamurthy in forum New To JavaReplies: 3Last Post: 05-08-2012, 12:16 PM -
Global variables
By nikkka in forum New To JavaReplies: 6Last Post: 03-16-2011, 09:10 AM -
1 of 8 global variables not visible
By Bobbo in forum New To JavaReplies: 7Last Post: 12-02-2010, 05:07 AM -
global variables
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-08-2009, 07:19 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks