Professional Documents
Culture Documents
Variables in Java - GeeksforGeeks
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.
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.
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
From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that
are:
In this way, a name can only be given to a memor y location. It can be assigned values in two ways:
Variable Initialization
Illustrations :
float simpleInterest;
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
Now let us discuss dif ferent t ypes of variables which are listed as follows:
1. Local Variables
2. Instance Variables
3. Static Variables
1. Local Variables
These variables are created when the block is entered, or the function is called and destroyed af ter exiting from
The scope of these variables exist s only within the block in which the variable is declared. i.e., we can access
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
A s instance variables are declared in a class, these variables are created when an object of the class is created
3. Static 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.
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.
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.
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
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
Must Read:
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
Like 256
Previous Next
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
Are static local variables allowed in Java? Final local variables in Java
01 01, Nov 10 05 25, Sep 17
15, Nov 10
Current difficulty :
Easy
GeeksforGeeks
Easy Normal Medium Hard Expert
Load Comments
feedback@geeksforgeeks.org
@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