Results 1 to 5 of 5

Thread: Java bans Global Variables

  1. #1
    Join Date
    Sep 2009
    Posts
    135

    Java bans Global Variables

    Hi, to all. I have just started to work on Java. I am finding it very interesting. I have earlier studied C, C++ and Visual Basics 6.0 programming languages. While studying Java I realized that Java does not supports Global Variables. What is the reason for this ? Why Java decied to not use Global Variables ?

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java bans Global Variables

    Yes its true, that Global variables are considered Bad in Java. Even I was confused when I came to know about this. There are a number of reasons for which Java does not support Global Variables. To tell you one of the reason is Adding state(Global) variables breaks referential transparency. In simple words, you can no longer can understand a statement or expression on its own.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Java bans Global Variables

    Java has a very Basic concept which states "Every Variable in Java must be declared within a class." Declaring a Global variable violets the concept and hence Java has Banned Global variables. But as a C++ programmer this would be a limitation for you. The solution is keyword static which indicates that a particular variable is a class variable rather than an instance variable. This makes it very clear that there would only be one copy of the variable. It would not allow creating multiple copies of that variable for every instance of the class.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Java bans Global Variables

    Creating a Global variable will thereby allow creating a copy of that variable for each instance of the class. This in Java is considered as a limitation because you are limiting the use fo your program to one instance. Also, Global variables creates collisions in namespace. To not allow such limitation Java has done away with the concept of Global variables.

  5. #5
    Join Date
    Feb 2009
    Posts
    96

    Re: Java bans Global Variables

    in JAVA you can make something global by the following ways

    declare the variable as a public variable, a static variable, or a private variable.

    you would declare it like so

    class thisOne{
    public int num;
    private int number;
    static int numb;

    public void some method{}

    and so on

    the public means it can be viewed at any stage of the class
    private means that it is back end only
    static means it can't be changed such as if you want a number to stay absolute.

Similar Threads

  1. Exporting or importing global variables from a DLL to an EXE
    By SALAZAR in forum Software Development
    Replies: 5
    Last Post: 28-05-2012, 02:26 PM
  2. About instance variables in java
    By Khaled11 in forum Software Development
    Replies: 1
    Last Post: 19-03-2010, 02:38 PM
  3. How To Store Global Variables?
    By ramsun in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 12:54 PM
  4. Global Variables in C sharp
    By Harper 21 in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 08:43 AM
  5. iText: page events and global variables
    By ASHER in forum Software Development
    Replies: 3
    Last Post: 24-10-2009, 08:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,872,832.30981 seconds with 17 queries