Java variable
In java three types of variables are there.
They are :
i) Local Variables
ii) Instance Variables
iii) Static Variables
i) Local Variables: In java a variable defined within a block or method or constructor is called local variable.
The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variable only within that block.
Initilisation of Local Variable is Mandatory.
ii) Instance Variables: In java Instance variables are non-static variables and are declared in a class outside of all the methods, constructors and blocks.
As 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.
Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
Initilisation of Instance Variable is not Mandatory. Its default values will be set based on the type of the variable (for eg : for int default value is 0 ,for String default value is null)
Instance Variable can be accessed only by creating objects.
iii) Static Variables: In java static variables are also known as Class variables.
These variables are declared same as instance variables, where as these variable are declared using 'static' keyword within a class outside of methods, constructors and blocks.
In java we can only have one copy of a static variable per class irrespective of how many objects we create.
Static variables are created at the start of program execution and destroyed automatically when execution ends.
Initialization of Static Variable is not Mandatory. Its default values will be set based on the type of the variable (for eg : for int default value is 0 ,for String default value is null)
If we access the static variable like Instance variable (through an object), the compiler will show the warning message and it won’t halt the program. The compiler will replace the object name to class name automatically.
If we access the static variable without the class name, Compiler will automatically append the class name.
To access static variables, we need not create an object of that class, we can simply access the variable as className.staticVariableName.
In java three types of variables are there.
They are :
i) Local Variables
ii) Instance Variables
iii) Static Variables
i) Local Variables: In java a variable defined within a block or method or constructor is called local variable.
The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variable only within that block.
Initilisation of Local Variable is Mandatory.
ii) Instance Variables: In java Instance variables are non-static variables and are declared in a class outside of all the methods, constructors and blocks.
As 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.
Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
Initilisation of Instance Variable is not Mandatory. Its default values will be set based on the type of the variable (for eg : for int default value is 0 ,for String default value is null)
Instance Variable can be accessed only by creating objects.
iii) Static Variables: In java static variables are also known as Class variables.
These variables are declared same as instance variables, where as these variable are declared using 'static' keyword within a class outside of methods, constructors and blocks.
In java we can only have one copy of a static variable per class irrespective of how many objects we create.
Static variables are created at the start of program execution and destroyed automatically when execution ends.
Initialization of Static Variable is not Mandatory. Its default values will be set based on the type of the variable (for eg : for int default value is 0 ,for String default value is null)
If we access the static variable like Instance variable (through an object), the compiler will show the warning message and it won’t halt the program. The compiler will replace the object name to class name automatically.
If we access the static variable without the class name, Compiler will automatically append the class name.
To access static variables, we need not create an object of that class, we can simply access the variable as className.staticVariableName.