Java Variable Types
Java Variable Types
Home Whiteboard AI Assistant Online Compilers Jobs Tools Articles Corporate Training Teach with us
Chapters Categories
SQL HTML CSS Javascript Python Java C C++ PHP Scala C# Tailwind CSS Node.js MySQL Mongo
Advertisement
-
Syntax
Following is the basic form of a variable declaration −
Here data type is one of Java's data types and variable is the name of the variable. To declare more than one variable of the specified
type, you can use a comma-separated list.
https://www.tutorialspoint.com/java/java_variable_types.htm 1/6
10/04/2025 23:02 Java Variable Types
The following are the three types of Java variables:
Local variables
Instance variables
Class/Static variables
Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the
method, constructor, or block.
Local variables are visible only within the declared method, constructor, or block.
There is no default value for local variables, so local variables should be declared and an initial value should be assigned before
the first use.
Open Compiler
Output
Following example uses age without initializing it, so it would give an error at the time of compilation.
Open Compiler
https://www.tutorialspoint.com/java/java_variable_types.htm 2/6
10/04/2025 23:02 Java Variable Types
}
}
Output
Instance variables are declared in a class, but outside a method, constructor or any block.
When a space is allocated for an object in the heap, a slot for each instance variable value is created.
Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is
destroyed.
Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an
object's state that must be present throughout the class.
Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it
is null. Values can be assigned during the declaration or within the constructor.
Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when
instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference.VariableName.
Open Compiler
import java.io.*;
https://www.tutorialspoint.com/java/java_variable_types.htm 3/6
10/04/2025 23:02 Java Variable Types
public static void main(String args[]) {
Employee empOne = new Employee("Ransika");
empOne.setSalary(1000);
empOne.printEmp();
}
}
Output
name : Ransika
salary :1000.0
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor
or a block.
There would only be one copy of each class variable per class, regardless of how many objects are created from it.
Static variables are rarely used other than being declared as constants. Constants are variables that are declared as
public/private, final, and static. Constant variables never change from their initial value.
Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either
public or private constants.
Static variables are created when the program starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since they must be available for
users of the class.
Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object
references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be
assigned in special static initializer blocks.
Static variables can be accessed by calling with the class name ClassName.VariableName.
When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables
are not public and final, the naming syntax is the same as instance and local variables.
Open Compiler
import java.io.*;
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";
Output
Note − If the variables are accessed from an outside class, the constant should be accessed as Employee.DEPARTMENT
https://www.tutorialspoint.com/java/java_variable_types.htm 4/6
10/04/2025 23:02 Java Variable Types
What is Next?
You already have used access modifiers (public & private) in this chapter. The next chapter will explain Access Modifiers and Non-Access
Modifiers in detail.
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
TRENDING TECHNOLOGIES
Git Tutorial
Ethical Hacking Tutorial
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial
SDLC Tutorial
Unix Tutorial
CERTIFICATIONS
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online PHP Compiler
https://www.tutorialspoint.com/java/java_variable_types.htm 5/6
10/04/2025 23:02 Java Variable Types
Online Bash Compiler
Online SQL Compiler
Online Html Editor
ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE | PRIVACY POLICY | REFUND POLICY |
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects.
https://www.tutorialspoint.com/java/java_variable_types.htm 6/6