Académique Documents
Professionnel Documents
Culture Documents
search
Questions
Tags
Users
Badges
Unanswered
Ask Question
16
5
java
tagged
java
233456
Community Bulletin
Join us for Stack Overflow Worldwide Meetups! Apr 28
pdfcrowd.com
feedback
10 Answers
To define Global Variable you can make use of static Keyword
active
oldest
votes
14
public class Global { public static int a; public static int b; } now you can access a and b from anywhere by calling Global.a; Global.b;
link | improve this answer edited Jan 11 '11 at 7:16 answered Jan 10 '11 at 12:04 Abi 1,027 2 13
Linked
I need to make a variable accessible to the whole program in Java How to create a list of global variables that can be accessible from different classes? java separate file for global variables Global variables in Java What design pattern should be used for a global configuration Global variable in Java with access to it in any class in a package How do I make an instance of an object accessible to multiple swing forms? Java
2 1 2
Is there any keyword like 'Global' in Java.....I din't get it aTJ Jan 10 '11 at 12:35 "Global" is the name of the class. Jerome Jan 10 '11 at 12:37 Be careful when doing this - when the Global class gets unloaded the variables will be undefined null. If you sell your app this will happen sooner or later and you keep looking for error everywhere but not there.... user387184 Nov 5 '11 at 19:55 The static keyword makes variables globally accessible, while their class is loaded. sjas Jan 29 at 13:22
Related
Should I be using global variables or passing the variables in java? Global keylogger in Java Why no global variables in java like C++? Global variables in Eclipse RCP
feedback
pdfcrowd.com
Global variables in java Accessing variables in other classes (Java) Local and global variables in javascript What exactly does static mean when declaring global variables in Java? Static Methods or Not? Global variables? How to use static global variables in JAVA Java - Acessing global variables & Arrays Java only allowing global variables to be static? synchronizing global variables in a servlet
15
That being said you could create a set of public static members in a class named Globals. public class Globals { public static int globalInt = 0; /// } but you really shouldn't :). Seriously .. don't do it.
link | improve this answer edited Jan 10 '11 at 15:42 Tom Hawtin - tackline 50.6k 5 49 113 answered Jan 10 '11 at 12:05 Mihai Claudiu Toader 4,134 3 12
What is the best way to store global/static variables in an advanced java game? Why are there no global variables in Java? Local and Global variables Global variables in Java Global & Local variables in Javascript
okay...I get it aTJ Jan 11 '11 at 6:17 as I have written above - when the class gets unloaded the variables loose their values to null and your app will crash. user387184 Nov 5 '11 at 19:57
feedback
Java Advantages of Converting Global Variables to Local Variables in Methods Java Play Framework Global Variables Declaring global variables in Java
Global EditText Variables Query about simulating global variables in java java separate file for global
pdfcrowd.com
public class UsesGlobals { private final Globals globals; public UsesGlobals(Globals globals) { this.globals = globals; } }
link | improve this answer answered Jan 10 '11 at 12:19 Peter Lawrey 82.8k 6 53 98
variables
True, though I think your use of "Globals" confuses things a bit :-). Something like "Parameters" would be more fitting since it's not necessarily Global. Mark Peters Jan 10 '11 at 15:46
feedback
Generally Global variable (I assume you are comparing it with C,Cpp) define as public static final like class GlobalConstant{ public static final CODE }
= "cd";
Does a global variable need to be final? Why? Konerak Jan 10 '11 at 12:05
pdfcrowd.com
"final" means "constant", so this is a global constant. duffymo Jan 10 '11 at 12:07 final is like const of C Jigar Joshi Jan 10 '11 at 12:07
1 1
If it is final, then we cannot change the value of CODE anywhere rt ??.. aTJ Jan 10 '11 at 12:09 no , thats what Global variable means for if you want to share that variable and change the data also then just remove final but it certainly depends how you want to share , where you are using and all , Jigar Joshi Jan 10 '11 at 12:13
feedback
public class GlobalClass { public static int x = 37; public static String s = "aaa"; } This way you can access them with GlobalClass.x and GlobalClass.s
link | improve this answer answered Jan 10 '11 at 12:05 Mark 2,090 5 22
feedback
There is no such thing as a truly global variable in Java. Every static variable must belong to some class (like System.out), but when you have decided which class it will go in, you can refer to it from everywhere loaded by the same classloader. Note that static variables should always be protected when updating to avoid race conditions.
link | improve this answer answered Jan 10 '11 at 12:11 Thorbjrn Ravn Andersen 27.5k 3 23 77
pdfcrowd.com
java doesnt provide global variable functionally but if you want to have a global variable concept, we can make use of static keyword Abi Jan 10 '11 at 12:25
feedback
There are no global variables in Java, but there are global classes with public fields. You can use static import feature of java 5 to make it look almost like global variables.
link | improve this answer answered Jan 21 '11 at 2:07 fastcodejava 11.1k 2 18 44
feedback
feedback
public static int global = 5; } you can call anywhere you want:
pdfcrowd.com
GlobalImpl.global // 5
link | improve this answer answered Jan 10 '11 at 12:06 hilal 6,015 1 11 37
feedback
As you probably guess from the answer there is no global variables in Java and the only thing you can do is to create a class with static members: public class Global { public static int a; } You can use it with Global.a elsewhere. However if you use Java 1.5 or better you can use the import static magic to make it look even more as a real global variable: import static test.Global.*; public class UseGlobal { public void foo() { int i = a; } } And voil! Now this is far from a best practice so as you can see in the commercials: don't do this at home
link | improve this answer answered Jan 11 '11 at 7:25 gabuzo 1,716 1 14
feedback
pdfcrowd.com
Your Answer
Name Email
or
log in
Home Page
Not the answer you're looking for? Browse other questions tagged java or ask your own question.
pdfcrowd.com
question feed
about | faq | blog | chat | data | podcast | shop | legal | advertising info | mobile | contact us | feedback
stackoverflow.com api/apps careers serverfault.com superuser.com meta area 51 webapps gaming ubuntu webmasters cooking game development math photography stats tex english theoretical cs programmers unix apple wordpress physics home improvement gis electronics android security bicycles dba drupal sharepoint scifi & fantasy user experience skeptics rpg
site design / logo 2012 stack exchange inc; user contributions licensed under cc-wiki with attribution required
rev 2012.4.15.2261
pdfcrowd.com