Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python Competitive Programming Machine Learnin

Variables in Java
Difficulty Level :
Easy ● Last Updated :
31 Dec, 2021

Variable in Java is a data container that saves the data values during Java program execution. Ever y variable is

assigned a data t ype that designates the t ype and quantit y of value it can hold. Variable is a memor y location name

of the data.

A variable is a name given to a memor y location. It is the basic unit of storage in a program.

The value stored in a variable can be changed during program execution.

A variable is only a name given to a memor y location, all the operations done on the variable ef fect s that memor y

location.

In Java, all the variables must be declared before use.

How to declare variables?

We can declare variables in java as pictorially depicted below as a visual aid.

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 1/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Start Your Coding Journey Now! Login Register

From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that

are:

1. Datat ype: Type of data that can be stored in this variable. 

2. Dataname : Name was given to the variable. 

In this way, a name can only be given to a memor y location. It can be assigned values in two ways: 

Variable Initialization

A ssigning value by taking input

How to initialize variables?

It can be perceived with the help of 3 component s that are as follows:


We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and
Got It !
understood our
Cookie Policy &
Privacy Policy
datat ype: Type of data that can be stored in this variable.

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 2/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

variable_name: Name given to the variable.

value: It is the initial value stored in the variable.

Start Your Coding Journey Now! Login Register

Illustrations : 

float simpleInterest;

// Declaring float variable

int time = 10, speed = 20;

// Declaring and Initializing integer variable

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and
Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 3/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

char var = 'h';

Start Your Coding Journey Now!


// Declaring and Initializing character variable Login Register

Types of Variables in Java

Now let us discuss dif ferent t ypes of variables which are listed as follows: 

1. Local Variables

2. Instance Variables

3. Static Variables

Let us discuss the trait s of ever y variable been up here in detail.

1. Local Variables 

A variable defined within a block or method or constructor is called a local variable. 

These variables are created when the block is entered, or the function is called and destroyed af ter exiting from

the block or when the call returns from the function.

The scope of these variables exist s only within the block in which the variable is declared. i.e., we can access

these variables only within that block.

Initialization of the local variable is mandator y before using it in the defined scope.

2. Instance Variables

Instance variables are non-static variables and are declared in a class out side any method, constructor, or block. 

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and
Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 4/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

A s 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.

Start Your Coding Journey Now!


Unlike local variables, we may use access specifier for instance variables. If we do not specif y any access
Login Register
specifier, then the default access specifier will be used.

Initialization of Instance Variable is not Mandator y. It s default value is 0

Instance Variable can be accessed only by creating object s.

3. Static Variables

Static variables are also known as Class variables. 

These variables are declared similarly as instance variables. The dif ference is that static variables are declared

using the static keyword within a class out side any method constructor or block.

Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many

object s we create.

Static variables are created at the star t of program execution and destroyed automatically when execution ends.

Initialization of Static Variable is not Mandator y. It s default value is 0

If we access the static variable like the Instance variable (through an object), the compiler will show the warning

message, which won’t halt the program. The compiler will replace the object name with the class name

automatically.

If we access the static variable without the class name, the compiler will automatically append the class name.

Differences between the Instance variable Vs. the Static variables

Now let us do discuss the dif ferences between the Instance variable Vs. the Static variables

Each object will have it s copy of the instance variable, whereas We can only have one copy of a static variable per
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and Got It !
class irrespective of how many understood our
Cookie
object s we Policy &
Privacy Policy
create.

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 5/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Changes made in an instance variable using one object will not be reflected in other object s as each object has

it s own copy of the instance variable. In the case of static, changes will be reflected in other object s as static

Start Your Coding Journey Now!


variables are common to all object s of a class.
Login Register
We can access instance variables through object references, and Static Variables can be accessed directly using

the class name.

Syntax : Static and instance variables

class GFG

// Static variable

static int a;

// Instance variable
int b;

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 6/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Variables in Java | GeeksforGeeks


Start Your Coding Journey Now! Login Register

Must Read: 

Scope of Variables in Java

Comparison of static keyword in C++ and Java

Are static local variables allowed in Java?

Instance Variable Hiding in Java

This ar ticle is contributed by Harsh Agar wal. If you like Geek sforGeek s and would like to contribute, you can also

write an ar ticle using write.geek sforgeek s.org or mail your ar ticle to review-team@geek sforgeek s.org. See your

ar ticle appearing on the Geek sforGeek s main page and help other Geek s. Please write comment s if you find

anything incorrect, or you want to share more information about the topic discussed above.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 7/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Start Your Coding Journey Now! Login Register

Like 256

Previous Next

Java Identifiers Scope of Variables In Java

RECOMMENDED ARTICLES
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and
Page : 1 2 3
Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 8/11


2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Are static local variables allowed in Java? Final local variables in Java
01 01, Nov 10 05 25, Sep 17

Start Your Coding Journey Now! Login Register


final variables in Java Static and non static blank final variables in
06
02 02, Nov 10
Java
08, Nov 17

Assigning values to static final variables in Using Variables in JShell of Java 9


03
Java 07 15, Apr 18

15, Nov 10

Difference between static and non-static


08
Scope of Variables In Java variables in Java
04 15, Jun 16 03, Apr 19

Ar ticle Contributed By : Vote for difficulty

Current difficulty :
Easy

GeeksforGeeks
Easy Normal Medium Hard Expert

Improved By : Pradeep-Kumar, JohnsonJ, jamshaidiqbal120, kushvillia, piyush bharti, causalcau, nishkarshgandhi,


amanmalakar007
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and Got It !
understood our
Cookie Policy &
Privacy Policy
Article Tags : java-basics, Java, School Programming
https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 9/11
2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Practice Tags : Java

Start Your Coding Journey Now! Login Register


Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

Load Comments

5th Floor, A-118,

Sector-136, Noida, Uttar Pradesh - 201305

feedback@geeksforgeeks.org

Company Learn Web Development Contribute


About Us Algorithms Web Tutorials Write an Article
Careers Data Structures HTML Write Interview Experience
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and Got It !
Privacy Policy Languages
understood CSS
our
Cookie Policy &
Privacy Policy Internships
https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 10/11
2/23/22, 9:31 PM Variables in Java - GeeksforGeeks

Contact Us CS Subjects JavaScript Videos


Copyright Policy Video Tutorials Bootstrap
Start Your Coding Journey Now! Login Register

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and
Got It !
understood our
Cookie Policy &
Privacy Policy

https://www.geeksforgeeks.org/variables-in-java/#:~:text=Variable in Java is a,given to a memory location. 11/11

You might also like