java variable
Last Updated on : 29th Mar 2023 19:57:56 PM
1) A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory location.
2) A variable is a container which holds the value while the program is executed. A variable is assigned with a data type.
How to declare variables?
variables can be declared as following way:
Syntax: dataType variableName;
Example: int result;
Here dataType shows that which type of value can be stored by the variable.
and variableName is the name given to the variable by the programmer.There are some naming rules are as follows:-
- All identifiers should begin with a letter (A to Z or a to z), ($) or an underscore (_).
- After the first character, variables can have any combination of characters.
- A keyword cannot be used as an identifier/variable.
- variables/ identifiers are case sensitive.
- Examples of valid identifiers: age, $salary, _name.
- Examples of invalid identifiers: 123abc, -name.
How to initialize variables?
Syntax: dataType variableName = value;
Example: int marks = 70;
Types of Variables?
There are three types of variables in Java:
- local variable
- instance variable
- static variable
1) Local Variable
A variable defined within a block or method or constructor is called a local variable.
- These variables are created when the block is entered, or the function is called and destroyed after exiting from the block.
- The scope of these variables exists only within the block in which the variables are declared, i.e., we can't access these variables outside of that block.
- Initialization of the local variable is mandatory before using it in the defined scope.
Example:-
import java.io.*;
class Demo{
public static String a= "Hello"; //static variable
public static void main (String[] args) {
//static variable can be accessed without object creation
System.out.println("Message is : "+Demo.a);
}
}
Output
Local Variable: 10
2) Instance Variable
Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block.
- 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.
- Instance variables can be accessed only by creating objects.
import java.io.*;
class Demo{
public String a; // Declared Instance Variable
public Demo()
{ // Default Constructor
this.a= "Hello"; // initializing Instance Variable
}
//Main Method
public static void main(String[] args)
{
// Object Creation
Demo msg= new Demo();
System.out.println("Message is: " + msg.a);
}
}
Output
Message is: Hello
3) Static variable
Static variables are also known as class variables.
- These variables are declared similarly as instance variables. The difference is that static variables are declared using the static keyword within a class outside of any method, constructor or block.
- Unlike instance variables, 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 a static variable is not mandatory. Its default value is 0.
import java.io.*;
class Demo{
public static String a= "Hello"; //static variable
public static void main (String[] args) {
//static variable can be accessed without object creation
System.out.println("Message is : "+Demo.a);
}
}
utput
Message is : Hello