Programmer Coding

variables:-

  1. Variable is manly use for memory location.
  2. Java have three type of variable

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

  1. Variable is manly use for memory location.
  2. Java have three type of variable :-
  • Local
  • Instance
  • Static
  1. 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.

 

  1. Public static void add(){

————————

//method body

————————

}

  1. Public static void add(int x){

————————

//method body

————————

}

  1. Public static int add(){

————————

//method body

————————

Return statement

}

  1. 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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top