- Variable is manly use for memory location.
- Java have three type of variable
Contents
Example of variables:-
public class airthmatic_operater {
public static void main(String[] args) {
int a,b;
a=20;
b=10;
System.out.println(a+b);
}
}
output:-
30
Variable in java
- Variable is manly use for memory location.
- Java have three type of variable :-
- Local
- Instance
- Static
- There are two type of java data type :-
- Primitive
- Non-primitive
Type of variable
Java have three type of variable :
- Local variable.
- Instance variable.
- Static variable.
-
Local variable
Â
When a variable is declared inside the method then it’s called local variable.
Syntax
Public static return_type function_name(parameter 1, parameter 2,………)
{
           // body of function
         // or set of statements
 }
Â
Example of local variable
 // Java Program to Illustrate Return Type
// In a Method
// Class
class progrming{
// method 1
// We are using void return type
public static void printAverage(int a, int b)
{
// Calculating average
int avg = (a + b) / 2;
// Printing the average of input numbers
System.out.print(avg);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Input numbers
int a = 15;
int b = 25;
// Calling method 1 inside main() method
printAverage(a, b);
}
}Â Output :-Â Â Â Â Â Â Â Â Â Â Â Â Â 20
Â
- Instance variable
When a variable is declared inside the class but outside the method then it’s called Instance variable.
Syntax :-
Â
public class person {
private string name;
private int age;
private double height;
}
Example of instance variable in java :-
Â
public class progremar {
// instance variables
String name;
int age;
// constructor
public progremercoding(String n, int a) {
name = n;
age = a;
}
// instance method
public void display() {
System.out.println(“Name: ” + name);
System.out.println(“Age: ” + age);
}
// main method
public static void main(String[] args) {
// create objects
progremer p1 = new progremar(“John”, 25);
progremar p2 = new progremar(“Jane”, 30);
// call instance method on objects
p1.display();
p2.display();
}
}
Â
output :-
       Name: John
Age: 25
Name: Jane
Age: 30
Â
Â
Â
Â
Â
Â
Â
-
Static variable
Â
When a variable is declared as static then it’s called static variable.
Syntax of static variable in java :-
- We can define static variable in 4 type which is given below.
- Public static void add(){
————————
//method body
————————
}
- Public static void add(int x){
————————
//method body
————————
}
- Public static int add(){
————————
//method body
————————
Return statement
}
- Public static int add(int y){
————————
//method body
————————
Return statement
}
Example of static variable in java :-
// Java program to demonstrate execution
// of static blocks and variables
class programercoding {
// static variable
static int a = m1();
// static block
static
{
System.out.println(“Inside static block”);
}
// static method
static int m1()
{
System.out.println(“from m1”);
return 20;
}
// static method(main !!)
public static void main(String[] args)
{
System.out.println(“Value of a : ” + a);
System.out.println(“programer coding”);
}
}
Â
Output :-
from m1
Inside static block
Value of a : 20
programer coding