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
🌐
Tutorialspoint
tutorialspoint.com › home › java › java variable types
Java Variable Types
September 1, 2008 - Java Vs. C++ ... A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within ...
Discussions

In Java, if a variable is declared but nothing is assigned to it, what is its value?
Static and instance variables are automatically initialized to zero, false, or null depending on the type. Local variables need to be explicitly initialized. That said, explicitly initializing variables is generally considered best practice to be obvious about your intent. More on reddit.com
🌐 r/learnprogramming
11
14
January 10, 2023
java - What are variables without public, private or protected declared as? - Stack Overflow
If instead of: private JButton theButton; I define a field like this: JButton theButton; What is the difference? More on stackoverflow.com
🌐 stackoverflow.com
Help making a colour instance variable in java?
Im fairly new to java, it is my first year learning it. I have an upcoming assignment that requires me to add a colour instance variable to one of my classes but I cant seem to do it without getting errors. More on reddit.com
🌐 r/learnprogramming
7
10
December 16, 2012
Instance variables in Kotlin and Java
val foo: String = "foo" in Kotlin is the same as private final String foo_field = "foo"; public String getFoo() { return foo_field; } Likewise, var foo: String = "foo" is the same as: private String foo_field = "foo"; public String getFoo() { return foo_field; } public void setFoo(value: String) { foo_field = value; } So you don't need to make your own backing fields and setters/getters, val and var include them for you. Check here for more info: https://kotlinlang.org/docs/properties.html The other thing is Kotlin does not have implicit defaults for types like Java does (like String's default value is null), so you must always initialize properties to a valid value. You usually initialize it when you declare the property but you can also do it in an init block. More on reddit.com
🌐 r/Kotlin
9
8
September 18, 2022
People also ask

So, what are the primary types of variables in Java?
In Java, variables are primarily categorized into three main types: Instance Variables: These belong to an instance of a class (an object). Each object has its own copy of these variables. Static (or Class) Variables: These are declared with the static keyword and are shared among all instances of a class. There's only one copy of a static variable per class. Local Variables: These are declared inside a method, constructor, or block. Their scope is limited to that block, and they are destroyed once execution leaves it. Method parameters are sometimes considered a fourth type, as they pass
🌐
upgrad.com
upgrad.com › home › blog › software development › types of variables in java: the building blocks of clean code
Types of Variables in Java: Code Smarter, Not Harder
What are some common mistakes developers make when using different types of variables in Java?
One common mistake is declaring variables as static just to access them without creating an object. This might seem convenient, but it often breaks encapsulation and leads to shared-state bugs. Another issue is the overuse of instance variables in methods that could work perfectly with local variables, leading to bloated object memory. Forgetting the scope and lifetime of the types of variables in Java is also a frequent cause of bugs and unexpected behaviors.
🌐
upgrad.com
upgrad.com › home › blog › software development › types of variables in java: the building blocks of clean code
Types of Variables in Java: Code Smarter, Not Harder
How do the types of variables in Java affect memory usage and performance in large applications?
Memory management is critical in large-scale systems. Local variables are stored on the stack and quickly released after method execution, making them very lightweight. Instance variables are stored in the heap and stay there as long as the object exists, so excessive or large instance variables can increase memory usage. Static variables, also stored in the heap, live for the entire duration of the application and can lead to memory leaks if not handled properly. Choosing the correct types of variables in Java can prevent memory bloat and garbage collection issues.
🌐
upgrad.com
upgrad.com › home › blog › software development › types of variables in java: the building blocks of clean code
Types of Variables in Java: Code Smarter, Not Harder
🌐
ScholarHat
scholarhat.com › home
Types of variables in Java with examples: Local, Instance & Static
A variable in Java is like a container that holds some data, such as a number or a word. You can use it to store values and change them when needed. Every variable has a name, a type (like int for numbers or String for words), and a value.
Published   August 30, 2025
🌐
Rootstack
rootstack.com › en › blog › java-variables
Types of variables in Java | Rootstack
Inside this container, each variable is assigned a data type that works to designate the type and amount of value that each variable can contain. In Java, variables can be classified into three main types: local variables, instance variables, and static variables (also known as class variables).
🌐
Upgrad
upgrad.com › home › blog › software development › types of variables in java: the building blocks of clean code
Types of Variables in Java: Code Smarter, Not Harder
August 13, 2025 - Variables in Java are core building blocks used to store and manage data in a program. Understanding the types of variables in Java, such as local, instance, and static, is essential for writing clean, efficient, and organized code.
Find elsewhere
🌐
Java Programming
java-programming.mooc.fi › part-5 › 3-primitive-and-reference-variables
Primitive and reference variables - Java Programming
Variables in Java are classified into primitive and reference variables. From the programmer's perspective, a primitive variable's information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › VariableBasics › declareVars.html
3.2. Declaring Variables in Java — AP CSA Java Review - Obsolete
To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon (;). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).
🌐
SitePoint
sitepoint.com › blog › java › beginning java: data types, variables, and arrays
Beginning Java: Data types, Variables, and Arrays — SitePoint
November 13, 2024 - Java’s data types are divided ... Classes, Interfaces, and Arrays). Variables in Java include Instance Variables (Non-Static Fields), Class Variables (Static Fields), Local Variables, and Parameters....
🌐
GeeksforGeeks
geeksforgeeks.org › java › variables-in-java
Java Variables - GeeksforGeeks
In Java, variables are containers used to store data in memory.
Published   November 11, 2025
🌐
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.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › variables.html
Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
The Java programming language defines the following kinds of variables: Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword.
🌐
Java Programming
java-programming.mooc.fi › part-1 › 4-variables
Variables - Java Programming
Boolean variables can either have the value true or false. When converting a string to a boolean, the string must be "true" if we want the boolean value to be true. The case is insensitive here: both "true" and "TRue" turn into the boolean value of true. All other strings turn into the boolean false. import java.util.Scanner; public class program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Write a boolean "); boolean value = Boolean.valueOf(scanner.nextLine()); System.out.println("You wrote " + value); } }
🌐
DataCamp
datacamp.com › doc › java › java-variables
Java Variables
In Java, variables are fundamental elements used to store data that can be manipulated throughout a program. A variable is essentially a container that holds data that can be changed during the execution of a program.
🌐
Geekster
geekster.in › home › java variables – types of variables
java variables - Types of Variables (with Example)
June 27, 2024 - Variables in Java are important objects that store data for manipulation and use throughout the program. There are three main types of variables: local, instance, and class (static) variables.
🌐
Learn Java
learnjavaonline.org › en › Variables_and_Types
Variables and Types - Learn Java - Free Interactive Java Tutorial
Java is a strong typed language, which means variables need to be defined before we use them. To declare and assign a number use the following syntax: ... In Java, a character is it's own type and it's not simply a number, so it's not common to put an ascii value in it, there is a special syntax ...
🌐
Medium
medium.com › @AlexanderObregon › understanding-java-variables-types-and-usage-92527dc20e11
Understanding Java Variables: Types and Usage
November 18, 2023 - Each variable in Java is associated with a specific data type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
🌐
GeeksforGeeks
geeksforgeeks.org › java › types-of-java-variables
Types of Java Variables - GeeksforGeeks
October 3, 2025 - In Java, variables are containers that store data values, such as numbers, text, or Boolean values.