An Instance variable in Java is used by Objects to store their states. Variables that are defined without the STATIC keyword and are Outside any method declaration are Object-specific and are known as instance variables. They are called so because their values are instance-specific and are not shared among instances.
If a class has an instance variable, then a new instance variable is created and initialized to a default value as part of the object creation of the class or subclass.
Example of Instance Variable
class Page {
public String pageName;
// instance variable with public access
private int pageNumber;
// instance variable with private access
}
Rules for Instance variable in Java
- Instance variables can use any of the four access levels
- They can be marked final
- They can be marked transient
- They cannot be marked abstract
- They cannot be marked synchronized
- They cannot be marked strictfp
- They cannot be marked, native
- They cannot be marked static
Cheatsheet
- Public, Private, Protected all access modifiers can be applied to Instance Variable(Default also).
- Instance Variable can be marked final.
- Instance Variable can be marked transient.
- Instance Variables cannot be abstract.
- Instance Variable cannot have a synchronized modifier.
- Instance Variable cannot have a strictfp modifier.
- Instance Variable cannot have the native modifier.
- Instance Variable cannot have a Static modifier as it will become a Class level variable. Meaning STATIC is one of the keyword which cannot be used with Instance variable.
- The instance variable will get a default value, which means the instance variable can be used without initializing it. The same is not true for Local Variable.
Instance Variable Type | Default Value |
---|---|
boolean | false |
byte | (byte)0 |
short | (short) 0 |
int | 0 |
long | 0L |
char | u0000 |
float | 0.0f |
double | 0.0d |
Object | null |
package com.jbt;
/*
* Here we will discuss about different type of Variables available in Java
*/
public class VariablesInJava {
/*
* Below variable is STATIC variable as it is outside any method and it is
* using STATIC modifier with it. It is using default access modifier. To
* know more about ACCESS MODIFIER visit appropriate section
*/
static String staticField;
/*
* Below variable is INSTANCE VARIABLE as it is outside any method and it is
* not using STATIC modifier with it. It is using default access modifier.
* To know more about ACCESS MODIFIER visit appropriate section
*/
int instanceField;
public static void main(String args[]) {
VariablesInJava obj = new VariablesInJava();
/*
* Instance variable can only be accessed by Object of the class only as below.
*/
System.out.println(obj.instanceField);
/*
* Static field can be accessed in two way.
* 1- Via Object of the class
* 2- Via CLASS name
*/
System.out.println(obj.staticField);
System.out.println(VariablesInJava.staticField);
System.out.println(new VariablesInJava().instanceField);
}
public void method() {
/*
* Below variable is LOCAL VARIABLE as it is defined inside method in
* class. Only modifier that can be applied on local variable is FINAL.
* To know more about access and non access modifier visit appropriate
* section.
*
* Note* : Local variable needs to initialize before they can be used.
* Which is not true for Static or Instance variable.
*/
final String localVariable = "Initial Value";
System.out.println(localVariable);
}
}
Get hold of Static Variable in Java to understand how it is different from the Instance variable.
Thanks for the info, it really means a lot to me, keep up the good work again, & looking for more in the future post of yours.
Thank you for the appreciation.
Really cool blog., you have done a well job on this topic.thanks. will read on
Why the ***** is this comment section so toxic and full of illegible broken english
Hi,
Thank you so much for making such a great website.
It made me understand the static topic very clearly.
is global variable or instance variable same or not?
There is no global variable concept in Java.
Is it possible to assign a value to an instance variable during declaration stage?
Yes. It is possible and completely valid.
class superEmployee { public String lastName = "assigned Value"; }
nice..
Thankz.. it is easy to understand.
Good Explanation.
This design is spectacular! You certainly know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job.
I really enjoyed what you had to say, and more than that, how you presented it.
Too cool!
good website for java, i will visit again
Previously i was having difficulty with all these topic , but now i m confident with these topic.
A lot of thanx to you….
Very useful to me…
more clarification about java programing in 1.Local variable 2.Instance variable 3.Static variable
and give the example by using demostration
it is really helpful for beginners to learn java basics..
is it possible for you to give some more examples for related topics.. thank you.. 🙂
We do try to add as much information as possible. And will keep doing the same.
Wat is d difference between class variable nd instance variable
Please check below link for better clarity on different types of variables.
http://94.143.139.145/core-java-tutorial/variables-in-java/
what is java????
Java is a high level programming language.It was developed by Sun Microsystem in 1995.
Good work .thank you
good work .than you
I want to follow you along, pls show concern.
confusing with lots of keywords, how to remember their usage..
No Matter how much are there you have to remember. 🙂
its good to read this
I want to learn java programming
Please like our page. Some times we organize online classes you can attend those classes if required.
Regards
good
why don’t use the these keywords like Abstract,strictfp,native.
someone please explain why can not use abstract ,strictfp,native
Yes, please.
Great Work, Nice Explaination, I Simply Understood The Difference
Instance variable can be access in this way too
System.out.println(new VariablesInJava().instanceField)
This is recommended when your calling Instance variable not more than once.
You said instance variables can be abstract, but its not true, why we need to mark any variable abstract?!
Typo problem. Same has been resolved.
what is the main use of constructor in java core.
Constructor is used to initialized value of object
To create an Object of the given class.
@Shweta, sister, a variable declared as “static”, makes it a class variable, not an instance variable. There’s a small and a simple difference between the two. 🙂
Hi Bharat,
instance variable can be static but you have mention it cannot be static ..as per my knowledge its wrong statement.
bharat: instance variables can’t be static ..if you define static variable just outside the method it can’t be instance variable….if you define variables with static keyword ..it becomes class variable(which is not instance variable..)..
class variables are not part of Object.where as instance variables are part of Object..Object stores its state in Non Static fields i.e instance variables also called ‘fields’
no instance variable cannot be static because we have learned in the rules of the instance variable
Static variables can also be accessed directly:
/*
* Static field can be accessed in two way.
* 1- Via Object of the class
* 2- Via CLASS name
*3-Directly calling the static variable itself(within static methods)
*/
System.out.println(obj.staticField);
System.out.println(VariablesInJava.staticField);
System.out.println(staticField);
You can’t declare an instance variable as abstract.The program wont compile.Please check
becz abstract can be only class and method not variable
I want learned java
Hi Bharat,
You said you want to learn Java. As you can see there are articles i have written on site which would be helpful for you.
Or if you want to direct learn Java by us do let me know. We are going to start online classes. Where you can start by enrolling, It will be paid classes.
Thanks
Sir I want learn java
Can you pleasexplain send me your contact profile to
For online class
Hi Sunil,
Right now i am not having enough time for online classes.
Regards
idiot lazy ur grown like a donkey cant u learn on ur own
Awesme xplanation
thanx
concept gt cleared
keep up the good wrk *–*
Can be abstract – Could you explain what it means for an instance variable to be abstract
nah i cant
**** u mate
lol r u idjot
nice it is……..i want learn from this ……….tihs is fist time i saw this……….
Galeej bwack!!!!!!!!!!!!!!!!!!!!!!!!!!!
nice it is……..i want learn from this
very good