To define Global Variable you can make use of static Keyword

public class Example {
    public static int a;
    public static int b;
}

now you can access a and b from anywhere by calling

Example.a;

Example.b;
Answer from Abi on Stack Overflow
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ business courses โ€บ business 104: information systems and computer applications
Java Global Variable: Declaration & Examples - Lesson | Study.com
January 7, 2018 - Let's recap. Global variables are not technically allowed in Java. A global variable is one declared at the start of the code and is accessible to all parts of the program. Since Java is object-oriented, everything is part of a class.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ global variable in java
Global Variable in Java - Scaler Topics
April 11, 2024 - Global variables are the variables that can be accessed anywhere in the java program. Java being a strictly object-oriented programming language doesnโ€™t have the concept of a global variable.
Discussions

Global variables in Java - Stack Overflow
Effective Java by Joshua Bloch, Chapter 4: Classes and Interfaces, Item 19: Use interfaces only to define types "The constant interface pattern is a poor use of interfaces." This book is worth reading! :) 2013-04-15T20:37:23.99Z+00:00 ... Additionally from JDK 1.5+ you can use static import for your global variables... More on stackoverflow.com
๐ŸŒ stackoverflow.com
When is it okay to declare global variables?
There is no better example of why global variables are "problematic" than this tale from Rinkworks I was asked about taking on a contract to maintain a piece of software. Something about the way it was presented made me wary. I asked to look over it first. What a sight! I use it as an example of why not to use global variables. Among other things, there were files with suites of functions on the following order: adjust_alpha() { alpha = gamma + offset * 3; } adjust_beta() { beta = gamma + offset * 3; } Dozens of functions that differed only by the global variable they modified. Just picture it: a multi-thousand line program with a graphical interface and a database that never used function parameters. The original programmer painted himself into a corner with his variable names. Clearly if you need variables "up," "down," "left," and "right," you name them as such. When he found himself needing those direction names in different parts of his program but was stuck because global variable names had to be unique, his solution was to use names like: up, _up, up_, Up, uP, UP, _Up, _UP down, _down, down_, Down, dOWN, DOWN, _Down, _DOWN ...and so on. Even the densest of my students comprehended immediately why that was bad. Needless to say, I turned down the job. Using global constants (static final) is generally OK. Edit: Escape underscores More on reddit.com
๐ŸŒ r/learnjava
14
22
February 12, 2020
[Java] Why are global variables bad?
A variable limited in scope to a single function has something less than a couple hundred lines in which it might be affected. A variable limited in scope to a single class only has less than a few thousand lines in which it might be affected. A variable limited in scope to a package or class heirarchy has something less than 10k lines of code in which it might be affected. A variable that is not limited in scope at all could have literally millions of lines of code that could possibly affect it. It's hard to reason about millions of lines of code. It gets much much worse when multiple threads may be acting on it. More on reddit.com
๐ŸŒ r/learnprogramming
14
9
July 7, 2011
What is a work around global variables in java. I need to use a global variable for assignment, but my professor says we cant use global variable.

Your professor is telling you not to use a global variable for a reason. Why you need to add values to an array list. Design your classes and instances around that why.

Professionally, we avoid global variables because they are bad design that can have nasty side effects.

More on reddit.com
๐ŸŒ r/javahelp
11
5
May 30, 2021
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 966997 โ€บ how-can-i-create-a-global-variable-in-java
How can I create a Global variable in Java? | Sololearn: Learn to code for FREE!
Java doesn't technically support global variables. As a pure object-oriented language, everything needs to be part of a class. The reason is to protect data and members of classes from being changed (inadvertently or on purpose) by other parts ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ using-global-variables-constants-in-java
Using Global Variables/Constants in Java
November 30, 2020 - Though, unfortunately, Java doesn't technically allow the creation of variables in a global scope.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ when is it okay to declare global variables?
r/learnjava on Reddit: When is it okay to declare global variables?
February 12, 2020 -

I've been told that it's generally good practice to avoid declaring variables globally, so I always try to declare everything in the smallest scope possible.

What is the problem with global variables and when is it okay to use them? Are there any good hard-and-fast rules on the topic?

Top answer
1 of 7
31
There is no better example of why global variables are "problematic" than this tale from Rinkworks I was asked about taking on a contract to maintain a piece of software. Something about the way it was presented made me wary. I asked to look over it first. What a sight! I use it as an example of why not to use global variables. Among other things, there were files with suites of functions on the following order: adjust_alpha() { alpha = gamma + offset * 3; } adjust_beta() { beta = gamma + offset * 3; } Dozens of functions that differed only by the global variable they modified. Just picture it: a multi-thousand line program with a graphical interface and a database that never used function parameters. The original programmer painted himself into a corner with his variable names. Clearly if you need variables "up," "down," "left," and "right," you name them as such. When he found himself needing those direction names in different parts of his program but was stuck because global variable names had to be unique, his solution was to use names like: up, _up, up_, Up, uP, UP, _Up, _UP down, _down, down_, Down, dOWN, DOWN, _Down, _DOWN ...and so on. Even the densest of my students comprehended immediately why that was bad. Needless to say, I turned down the job. Using global constants (static final) is generally OK. Edit: Escape underscores
2 of 7
5
Java doesn't have truly global variables. But in general the issue is that the greater the scope of the variable the greater the chance you'll run into conflicts. (E.g. you'll import a library that uses a global variable named the same as the one your project uses)
๐ŸŒ
W3Docs
w3docs.com โ€บ java
Global variables in Java
In Java, a global variable is a variable that is accessible from any part of the program. In Java, there is no such thing as a true global variable, as all variables must be declared within a class and are only accessible within the scope of ...
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @ankit92nita โ€บ lets-explore-local-and-global-variables-in-java-with-real-time-use-cases-dd747fe917b0
Letโ€™s explore local and global variables in Java with real-time use cases. | by Ankit | Medium
October 1, 2024 - Ans:- They are not mandatory to initialize, if we did not initialize, NM will take default values based on data types. byte, short, int and long โ€” โ€” โ€” โ€” โ†’ 0 ... Note: โ€” global variable can be public default , private, protected and final.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ local-and-global-variables
Local and Global Variables - GeeksforGeeks
March 21, 2024 - Lifetime Limited to Scope: Local ... is required. Global variables are variables that are declared outside of any function or block of code and can be accessed from any part of the program....
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-global-variables-in-Java-and-C-C
What is the difference between global variables in Java and C/C++? - Quora
Answer (1 of 2): In Java, global variables are those that are declared at the class level and are accessible to all methods in the class. In C/C++, global variables are those that are declared outside of any function or block and can be accessed ...
๐ŸŒ
Quora
quora.com โ€บ What-are-global-variables-in-Java
What are 'global variables' in Java? - Quora
Answer (1 of 10): Global varriable which is defined after the class tag and before Method. so there are two type of Global variable 1. Instace global variable [code]public class GolbalVariable{ public String username; public String password; } [/code]Explanation : this is called as instance vari...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ random โ€บ global variables in java: when to use them?
Global variables in Java: when to use them?
March 3, 2025 - Global variables are variables that are accessible from anywhere in the application. In other words, their scope is the entire application. To create such a variable in Java, you need to create a public static variable in a public class:
๐ŸŒ
Verve AI
vervecopilot.com โ€บ interview-questions โ€บ what-do-you-really-need-to-know-about-java-global-variable-for-your-next-tech-interview
What Do You Really Need To Know About Java Global Variable For Your Next Tech Interview
August 15, 2025 - At its core, a java global variable refers to variables declared at the class level, outside of any specific method, constructor, or block [^1]. Unlike local variables, which live only within the method or block they're declared in, a java global ...
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ global_variables_in_java
Global variables in Java
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
Quora
quora.com โ€บ What-is-the-scope-of-local-variables-in-Java-Are-they-global-by-default-if-they-are-not-declared-as-private-or-protected
What is the scope of local variables in Java? Are they global by default if they are not declared as 'private' or 'protected'? - Quora
Answer: Local variables in Java are always declared in the body of methods without access modifiers and are never global and disappear after the method call ends. Class data members in the other hand can have access modifier private, protected ...
๐ŸŒ
C2
c2.com โ€บ cgi โ€บ wiki
Global Variables Are Bad
This site uses features not available in older browsers
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_variables.asp
Java Variables
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Variables are containers for storing data values.
๐ŸŒ
Devzery
devzery.com โ€บ post โ€บ guide-to-variable-global-java
Guide to Global Variables in Java: Unravel Mysteries
September 21, 2024 - The Singleton pattern is another alternative to global variables. By creating a single instance of a class and providing global access to it, you can achieve the benefits of global variables while maintaining better control over data access and modification. Hereโ€™s an example of the Singleton pattern in Java: