Java Variables
Java Variables
•••
In Java, Variables are the data containers that save the data values during Java
program execution. Every Variable in Java is assigned a data type that designates
the type and quantity of value it can hold. A variable is a memory location name
for the data.
Variables in Java
Java variable is a name given to a memory location. It is the basic unit of storage
in a program.
• The value stored in a variable can be changed during program
execution.
• Variables in Java are only a name given to a memory location. All the
operations done on the variable affect that memory location.
• In Java, all variables must be declared before use.
class GFG {
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10;
Output
Local Variable: 10
Example :
Java
package a;
public class LocalVariable {
public static void main(String[] args)
{
// x is a local variable
int x = 10;
if (x > 5) {
// result is a
// local variable
String result = "x is greater than 5";
System.out.println(result);
}
Output :
message = Hello, world!
x is greater than 5
Iteration 0
Iteration 1
Iteration 2
2. Instance Variables
Instance variables are non-static variables and are declared in a class outside of
any method, constructor, or block.
• As instance variables are declared in a class, these variables are created
when an object of the class is created and destroyed when the object is
destroyed.
• Unlike local variables, we may use access specifiers for instance
variables. If we do not specify any access specifier, then the default
access specifier will be used.
• Initialization of an instance variable is not mandatory. Its default value
is dependent on the data type of variable. For String it
is null, for float it is 0.0f, for int it is 0, for Wrapper classes like Integer it
is null, etc.
• Instance variables can be accessed only by creating objects.
• We initialize instance variables using constructors while creating an
object. We can also use instance blocks to initialize the instance
variables.
The complexity of the method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
Java
// Java Program to demonstrate
// Instance Variables
import java.io.*;
class GFG {
// Main Method
public static void main(String[] args)
{
// Object Creation
GFG name = new GFG();
// Displaying O/P
System.out.println("Geek name is: " + name.geek);
System.out.println("Default value for int is "
+ name.i);
Output
Geek name is: Shubham Jain
Default value for int is 0
Default value for Integer is null
3. Static Variables
Static variables are also known as class variables.
• These variables are declared similarly to instance variables. The
difference is that static variables are declared using the static keyword
within a class outside of any method, constructor, or block.
• Unlike instance variables, we can only have one copy of a static variable
per class, irrespective of how many objects we create.
• Static variables are created at the start of program execution and
destroyed automatically when execution ends.
• Initialization of a static variable is not mandatory. Its default value is
dependent on the data type of variable. For String it is null, for float it
is 0.0f, for int it is 0, for Wrapper classes like Integer it is null, etc.
• If we access a static variable like an instance variable (through an
object), the compiler will show a warning message, which won’t halt the
program. The compiler will replace the object name with the class name
automatically.
• If we access a static variable without the class name, the compiler will
automatically append the class name. But for accessing the static
variable of a different class, we must mention the class name as 2
different classes might have a static variable with the same name.
• Static variables cannot be declared locally inside an instance method.
• Static blocks can be used to initialize static variables.
The complexity of the method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
Java
// Java Program to demonstrate
// Static variables
import java.io.*;
class GFG {
// Declared static variable
public static String geek = "Shubham Jain";
Output
Geek Name is : Shubham Jain
// Instance variable
int b;
}
Conclusion
The Important points to remember in the articles are mentioned below:
• Variables in Java is a data container that saves the data values during
Java program execution.
• There are three types of variables in Java Local variables, static
variables, and instance variables.
FAQs on Variables in Java
Q1. What are variables in Java?
Variables are the containers in Java that can store data values inside them.
Q2. What are the 3 types of variables in Java?
There are three types of variables in Java are mentioned below:
1. Local Variables
2. Static Variables
3. Instance Variables
Q3. How to declare variables in Java examples?
We can declare variables in java with syntax as mentioned below:
data_type variable_name;
Example:
// Integer datatype with var1 name
int var1;