Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Are global variables allowed in Java programming or not?

user-image
Question added by Syed Muhammad Hassaan , Installation Engineer , Infosys Management
Date Posted: 2015/07/15
Iqbal Hossain
by Iqbal Hossain , Asst. General Manager , Green Bird Properties Ltd

No ... there are no global variable allowed in Java 

Nasry Al-Haddad
by Nasry Al-Haddad , software engineer , Element^n

In addition to the other answer, you should also consider System.setProperty() and System.getProperty(), though I personally wouldn't go for this option if there are more than very few variables.

In case there are several global variables, use the method suggested in the other answer if they are never changed.

But if the variables are subject to change (not programmatically), I suggest you use a properties file which the GlobalClass reads from. This would prevent rebuilding your application when the values have to be changed.

mahmoud mohammed mansour mohammed mansour
by mahmoud mohammed mansour mohammed mansour , Software Developer , Crystal Mind

Global Variable in java be on Level of Class only

and to define variable global you can use it in any position of Your Application you Define Variable as Static and you can access it from any position.

Example :-

publicclassGlobalClass{publicstaticint age;publicstaticstring name;}GlobalClass.age;GlobalClass.name;

More Questions Like This