๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_variables.asp
Java Variables
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ types-of-java-variables
Types of Java Variables - GeeksforGeeks
October 3, 2025 - Example: This example demonstrates the use of instance variables, which are declared within a class and initialized via a constructor, with default values for uninitialized primitive types. ... import java.io.*; class Geeks { // Declared Instance Variable public String geek; public int i; public Integer I; public Geeks() { // Default Constructor // initializing Instance Variable this.geek = "Sweta Dash"; } // Main Method public static void main(String[] args) { // Object Creation Geeks name = new Geeks(); // Displaying O/P System.out.println("Geek name is: " + name.geek); System.out.println("Default value for int is "+ name.i); // toString() called internally System.out.println("Default value for Integer is: "+ name.I); } }
Discussions

What are types in Java ?
Types as in it's a custom datatype blueprint, a type of structure created by you to store data. I understood it that way from typescript interface implementation of model classes. Like Maps, Sets and List which are interface you can build your own data structure. More on reddit.com
๐ŸŒ r/learnjava
18
0
August 15, 2024
When it's good practice to use var instead of explicit name of the variable
When it improves readability of your code. From my experience it does that very seldom because it needs to be cases where the data type isn't really relevant. This only happens when for example working with a third party or java API where you have a variable that is only used by sending it back to the same API or returns something relevant later. For example lets say that we have a List that we use Stream on but want to hold it in a variable to do conditional operations on. We could use Stream stream but var stream gets the point across without adding complexity for the reader. Another example, lets take resilience4j https://resilience4j.readme.io/docs/examples var circuitBreakerConfig = CircuitBreakerConfig.custom() ... .build(); CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig); Does knowing the full type of the Config object help the reader? I don't think so and it saves a lot of duplicated words. The Registry might be used elsewhere or returned so that's not as clear if it should be var. Is this necessary? Not really, but it can be neat. More on reddit.com
๐ŸŒ r/java
134
81
October 25, 2021
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java variable types
Java Variable Types
September 1, 2008 - Discover the different types of variables in Java, including primitive and reference types, with examples and best practices.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ variables-in-java
Java Variables - GeeksforGeeks
From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are data type of the variable and name. It can be perceived with the help of 3 components explained above: ... Example: Here, we are initializing variables of different types like float, int and char.
Published ย  November 11, 2025
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ variables-in-java
Types of Variables in Java with Examples - DataFlair
April 16, 2024 - Variables in Java is used to store the value of element. There are 3 types of variable: Local, global, static. Learn each type with example and declaration
๐ŸŒ
Rootstack
rootstack.com โ€บ en โ€บ blog โ€บ java-variables
Types of variables in Java | Rootstack
In Java, variables can be classified into three main types: local variables, instance variables, and static variables (also known as class variables). Local variable: These variables are declared and used within a specific method, constructor, or block of code.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csjava โ€บ Unit1-Getting-Started โ€บ topic-1-3-variables.html
1.3. Variables and Data Types โ€” CS Java
There are two types of variables in Java: primitive variables that hold primitive types and object variables that hold a reference to an object of a class. A reference is a way to find the object (like a UPS tracking number helps you find your package).
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @nakulmitra2114 โ€บ types-of-variables-in-java-9535991fea4e
Types of Variables in Java. Types of Variables in Java | by Nakul Mitra | Medium
September 1, 2024 - In this example, numberOfCars is a static variable that is shared among all Car objects. Each time a new Car object is created, the value of numberOfCars increases. In Java, variables can be classified into three types based on their scope, lifetime, and behavior:
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ java-variables
Java Variables
Java variables are categorized into three main types: Local Variables: Declared inside a method or block and can only be accessed within that method or block. Instance Variables: Declared inside a class but outside of any method.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ variables-literals
Java Variables and Literals (With Examples)
Note: The floating-point literals are used to initialize float and double type variables. Character literals are unicode characters enclosed inside single quotes. For example, ... Here, a is the character literal. We can also use escape sequences as character literals. For example, \b (backspace), \t (tab), \n (new line), etc. A string literal is a sequence of characters enclosed inside double-quotes. For example, String str1 = "Java Programming"; String str2 = "Programiz";
๐ŸŒ
CodeHS
codehs.com โ€บ tutorial โ€บ 12380
Variables in Java
Click on one of our programs below to get started coding in the sandbox! ... Learn how to store data in Java.
Top answer
1 of 7
381
a.getClass().getName()
2 of 7
58

Expanding on Martin's answer...

Martins Solution

a.getClass().getName()

Expanded Solution

If you want it to work with anything you can do this:

((Object) myVar).getClass().getName()
//OR
((Object) myInt).getClass().getSimpleName()

In case of a primitive type, it will be wrapped (Autoboxed) in a corresponding Object variant.

Example #1 (Regular)

private static String nameOf(Object o) {
    return o.getClass().getSimpleName();
}

Example #2 (Generics)

public static <T> String nameOf(T o) {
    return o.getClass().getSimpleName();
}

Note: Anything below here does not directly answer the posed question. I added the following examples to provoke thought in those who are potentially looking for variable class names for one reason or another.


Getting the Instance using Pattern Matching

If statement (as of Java 17)

if(object instanceof String str)
    System.out.println(str);

Switch statement (as of Java 21)

switch(object) {
    case null -> System.out.println("The provided object is null.");
    case String str -> System.out.println("String encountered. The value is: " + str);
    case Integer myInt when myInt > 0 -> System.out.println("Integer greater than 0 encountered. The value is: " + myInt);
    case Integer myInt when myInt <= 0 -> System.out.println("Integer less than or equals to 0 encountered. The value is: " + myInt);
    default -> System.out.println("Unexpected Object encountered.");
}

Totally unnecessary and over the top example

Do with this what you will, but don't take the example as gospel. I am only showing possibilities here and you should make your own judgements on how to use these newer features.

public static class MyObject {
    public final String myField;
    public MyObject(String myField) { this.myField = myField; }
    @Override public String toString() { return "{ myField: " + this.myField + "}"; }
}

public static record MyRecord(String myField) {
    @Override public String toString() { return "{ myField: " + this.myField + "}"; }
}

public <T> String example(T object) {
    return switch(object) {
        case null -> "NULL";
        case String str -> "String: " + str;
        case Integer iNt -> "Integer: " + iNt;
        case Long lOng -> "Long: " + lOng;
        case MyObject myObject -> MyObject.class.getSimpleName() + ": " + myObject.toString();
        case MyRecord myRecord -> myRecord.getClass().getSimpleName() + ": " + myRecord.toString();
        default -> "UNKNOWN";
    };
}

More reading about pattern matching in switch statements: https://openjdk.org/jeps/441

Additional Learning

  • Material on Java Types, Values and Variables: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
  • Autoboxing and Unboxing: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
  • Docs on Pattern Matching for instanceof: https://docs.oracle.com/en/java/javase/14/language/pattern-matching-instanceof-operator.html
  • A good talk about history of some of these things: https://www.youtube.com/watch?v=wo84LFzx5nI
๐ŸŒ
Shiksha
shiksha.com โ€บ home โ€บ it & software โ€บ it & software articles โ€บ programming articles โ€บ understanding variables in java
Understanding Variables in Java - Shiksha Online
September 3, 2024 - Static (or Class) Variables: Declared as static, which means they are associated with the class, not individual objects. All instances of the class share the same static variable. ... A variable in Java is declared by first specifying its data type, followed by the variable name.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java variables
Java Variables - Scaler Topics
March 5, 2024 - These variables can be of various types such as local, instance, and static. Each variable in Java has a data type. These types of variables in Java are used to determine the size and layout of that variable.
๐ŸŒ
Java Programming
java-programming.mooc.fi โ€บ part-1 โ€บ 4-variables
Variables - Java Programming
A variable's type determines the types of values that can be assigned to it. String types hold text, int types whole numbers, double floating-point numbers, and boolean types are either true or false.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ understanding-java-variables-types-and-usage-92527dc20e11
Understanding Java Variables: Types and Usage
November 18, 2023 - CamelCase for Multiword Names: When a variable name consists of multiple words, the first letter of each inner word should be capitalized, e.g., myAge, finalResult. Java has two categories of data types that can be assigned to variables:
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ types-of-variables-in-java
Types of Variables in Java
July 7, 2023 - Depending on the kind of variable, they can either be initialized at the time of definition or afterward. In Java, there are primarily three types of variables: static variables, instance variables, and local variables.
๐ŸŒ
Learn Java
learnjavaonline.org โ€บ en โ€บ Variables_and_Types
Variables and Types - Learn Java - Free Interactive Java Tutorial
It is built on top of basic variable types called primitives. ... 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 for chars:
๐ŸŒ
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.