Variables
The term variable is widely used in Java programming language. It describes variable naming rules and conventions, literals, character strings, arrays, primitive types etc.
Java uses term "variable" which represents some kind of information. Objects are used to store their state in some fields. It is required to declare all the variables before you use it in your programming. You can say variable is a name given to someone who represents basic data type, default values, literals.
Declaring Variables :
In java it is required to declare variable first before you use it in your program. You can store numeric as well as string value into your variables. Variables are declared of some types. These types are your data types like int, byte, float, char etc. We use these keywords in declaration of variables to set type of variables.
Example :
- int num;
- char ch;
- String str;
Here, num is int type variable, ch is char type variable , and str is String type variable.
Variable Initialization :
You can initialize variable by assigning some valid value to the declared variable.
Example :
- int num=10;
- char ch='R';
- String str="Roseindia";
Java categorizes the variables in the following ways :
1)Instance variables (Non-Static Fields) - These are the variables which are defined within the body of the class and outside any method's body. They don't use static field modifier so they can be used by all the methods of a class which are not static. It is created when you create an object by using keyword "new" and when your object is destroyed, it is automatically destroyed.
Example :
package variable; class InstanceVariable { public int roll; public String name; public void setRoll(int roll) { this.roll = roll; } // The name variable is assigned in the constructor. public InstanceVariable(String studName) { name = studName; } public void display() { System.out.println("Roll No : " + roll); System.out.println("Name : " + name); } public static void main(String args[]) { InstanceVariable variableClass = new InstanceVariable("Roxi"); variableClass.setRoll(10); variableClass.display(); } }
Output :
Roll No : 10 Name : Roxi
2)Class variables (Static Fields)- These variables must have the keyword static and are declared inside the body of the class and outside any method body. The class variables can be accessed directly through the class or through an object instance. These variables are not serialized.
package variable; class ClassVariable { // int variable is a private static variable private static int roll; // NAME is a constant public static final String NAME = "Roxi"; public static void main(String args[]) { roll = 10; System.out.println("Roll : " + roll + " and Name: " + NAME); } }
Output :
Roll : 10 and Name: Roxi
3)Local Variables- This variable is having the most limited scope. It is accessible only from the function or block in which it is declared. Only final modifier can be used to declare any local variable.
package variable; class LocalVariable { public static void main(String[] args) { System.out.println("This is local variable example : "); display(); } static void display() { int a = 10, b = 20; System.out.println("Local variables a= " + a + " and b= " + b); } }
Output :
This is local variable example : Local variables a= 10 and b= 20