Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By pbrockway2
  • 2 Post By Tolls

Thread: No Global Variables In Java ?

  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Question 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!";
    }
    Java 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"?

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default 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

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.
    Zyril likes this.

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default 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

    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!

  6. #6
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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)?

  7. #7
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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...

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,745
    Rep Power
    19

    Default Re: No Global Variables In Java ?

    Quote Originally Posted by SnakeDoc View Post
    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.
    Zyril and Fubarable like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: No Global Variables In Java ?

    Thanks for correcting me! I always learn something new here!

  10. #10
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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!

Similar Threads

  1. Variable Storage and Global Variables in Java
    By lsrinivasamurthy in forum New To Java
    Replies: 3
    Last Post: 05-08-2012, 12:16 PM
  2. Global variables
    By nikkka in forum New To Java
    Replies: 6
    Last Post: 03-16-2011, 09:10 AM
  3. 1 of 8 global variables not visible
    By Bobbo in forum New To Java
    Replies: 7
    Last Post: 12-02-2010, 05:07 AM
  4. global variables
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-08-2009, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •