A variable is an identifier whose value will be changed during execution of the program
Rules for writing variables
- First letter must be an alphabet.
- The length of the variable should not exceed more than 32 characters.
- No special symbols are allowed except underscore.
- No keywords should use as variable names.
Types of variables in JAVA:
Whenever we develop any JAVA program that will be developed with respect to class only.
In a class we can use 'n' number of data members and 'n' number of methods.
Generally in JAVA, we can use two types of data members or variables. They are instance/non-static variables and static variables.
INSTANCE/NON-STATIC VARIABLES
- An instance variable is one whose memory space is creating each and every time whenever an object is created.
- Programmatically instance variable declaration should not be preceded by keyword static.
- Data type v1, v2?vn;
- Instance variable must be accessed with respect to object name i.e., objname.varname;
- Value of instance variable is not sharable.
- Instance variable are also known as object level data members since they are dependent on objects.
STATIC VARIABLES
- Static variables are whose memory space is creating only once when the class is loaded by class loader subsystem (a part of JVM) in the main memory irrespective of number of objects.
- Programmatically static variable declaration must be preceded by keyword static.
- Static data type v1, v2?vn;
- Static variables must be accessed with respect to class name i.e., classname.varname;
- Value of static variable is always recommended for sharable.
- Static variable are also known as class level data members since they are dependent on classes.