Variable in Java
What is a Variable?
When we want to store any information, we store it in an address of the computer. Rather of remembering the complex address where we have stored our information, we name that address.The naming of an address is known as variable. Variable is the name of memory location. In other words, variable is a name which is used to store a value of any type during program execution. To declare the variable in Java, we can use following syntax. Here, datatype refers to type of variable which can any like: int, float etc. and variableName can be any like: empId, amount, price etc . Java Programming language defines mainly three kind of variables.
Instance Variables
Static Variables (Class Variables)
Get Programming Knight’s stories in your inbox
Join Medium for free to get updates from this writer.
Local Variables.
Instance variables in Java
Instance variables are variables that are declare inside a class but outside any method, constructor or block. Instance variable are also variable of object commonly known as field or property. They are referred as object variable. Each object has its own copy of each variable and thus, it doesn’t effect the instance variable if one object changes the value of the variable. Here name and age are instance variable of Student class.
Static variables in Java.
Static are class variables declared with static keyword. Static variables are initialized only once. Static variables are also used in declaring constant along with the final keyword. Here institute code is a static variable. Each object of the Student class will share institute code property.
Additional points on static variable:
static variable are also understood as a class variables. static means to remain constant. In Java, it means that it will be continuous for all the instances created for that class. static variable need not be called from object. It is called by classname.static _ variable_name.
Note: A static variable can never be defined inside a method i.e it can never be a local variable.
Local variables in Java
Local variables are declared in method, constructor or block. Local variables are initialized when method, constructor or block starts and will be destroyed once its end. Local variable reside in stack. Access modifiers are not used for local variable.






