Global variables in Java
-
Possible Duplicates: http://stackoverflow.com/questions/5581234/why-are-there-no-global-variables-in-java http://stackoverflow.com/questions/4646577/global-variables-in-java Hi, Is there any way to declare global Variables in java? or something with a wide scope like them? can anybody explain me why are global variables considered bad? any articles about this are really appreciated. thank you
-
Answer:
You can create 'global' variables by declaring them within the scope of the class ie public class Example { String globalString = "this is global"; public static void main(String[] args){ String localString = "this is a local variable"; } } You can also create static variables which can be accessed by a method which has been declared static too.
user718238 at Stack Overflow Visit the source
Other answers
A good discussion can be found here, http://stackoverflow.com/questions/4222044/static-methods-or-not-global-variables/4222084#4222084
Adeel Ansari
On any class, you can declare static variables: class MyClass { public static String MyString = "Some String"; ... } And then reference them via: MyClass.MyString;
BigSean
public static will give enough scope to your variable for them to act as global variable. But think twice before doing so.
kunal
Related Q & A:
- What do we call the "middle" variables in a complex research model?Best solution by Cognitive Sciences
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- What is the dangers of Global Variables in Drupal specifically?Best solution by Quora
- How can I pass global variables into a function?Best solution by Stack Overflow
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.