There are three types of variables: 1. local, 2. instance and 3. static.
There are two types of datatypes in java, 1. primitive and 2. non-primitive.
Variable:
Variable is name of reserved area allocated in memory.
int var = 10;//Here var is variable
Types of Variable:
There are three types of variables in java
Local Variable
A variable that is declared inside the method is called local variable.
public class TestDemo{ public void calc(){ int sum = 0; sum = sum + 17; System.out.println("Total sum is : " + sum); } public static void main(String args[]){ TestDemo test = new TestDemo(); test.calc(); } }
This would produce following result:
Following example uses sum without initializing it, so it would give an error at the time of compilation.
public class TestDemo{ public void calc(){ int sum; sum = sum + 17; System.out.println("Total sum is : " + sum); } public static void main(String args[]){ TestDemo test = new TestDemo(); test.calc(); } }
This would produce following result:
Instance Variable
A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.
import java.io.*; public class Employee{ // this instance variable is visible for any child class. public String name; // salary variable is visible in Employee class only. private double salary; // The name variable is assigned in the constructor. public Employee (String empName){ name = empName; } // The salary variable is assigned a value. public void setSalary(double empSal){ salary = empSal; } // This method prints the employee details. public void printEmp(){ System.out.println("name : " + name ); System.out.println("salary :" + salary); } public static void main(String args[]){ Employee empOne = new Employee("Dinesh Rajput"); empOne.setSalary(70000); empOne.printEmp(); } }
This would produce following result:
Static variable
A variable that is declared as static is called static variable. It cannot be local.
import java.io.*; public class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development "; public static void main(String args[]){ salary = 70000; System.out.println(DEPARTMENT+"average salary:"+salary); } }
This would produce following result:
Note: If the variables are access from an outside class the constant should be accessed as Employee.DEPARTMENT
In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
The type is one of Java’s datatypes. The identifier is the name of the variable. To declare more than one variable of the specified type, use a comma-separated list.
Here are several examples of variable declarations of various types. Note that some include an initialization.
int a, b, c; // declares three ints, a, b, and c. int d = 3, e, f = 5; // declares three more ints, initializing // d and f. byte z = 22; // initializes z. double pi = 3.14159; // declares an approximation of pi. char x = 'x'; // the variable x has the value 'x'.
class Test{ int data=50;//instance variable static int m=100;//static variable void method(){ int n=90;//local variable } }//end of class
Data Types in Java:
In java, there are two types of data types
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…