Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
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 ?
Reply With Quote
  #2  
Old 09-11-2009
Member
 
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.
Reply With Quote
  #3  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #4  
Old 09-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
  #5  
Old 10-11-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:57 AM.