top of page
Writer's pictureUrvi Patel

Variables in Java

Variables are the data containers that save data vaules. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.


Java variable is a name given to a memory location. It is the basic unit of storage in a program.

  • All variables must be declared before use.

  • The value stored in a variable can be changed during program execution.

  • Variables in Java are only a name given to a memory location. All the operations done on the variable affect that memory location.


Declaration of variables:

data_type Name_of_variable

int value;

data_type : Type of the data that can be stored into this variable.

Name_of_variable : Name which is given to variable. A name is only given to memory location. We can assigned a values in 2 different ways:

  • Variable Initialization int age = 10; data_type variable_name Value

  • Assigning value by taking input


Types of Varibles: There are 3 different types of variables. Lets see discuss it with example: 1. Local Variables 3. Static Variables

2. Instance Variables

1. Local Variables: A variable declared inside the body of the method or consturctor or within a blocak is called "Local Variable". We can only use this variable within that method and also visible within the declared method. There is no default value for local variables, Initialization of the local variable is mandatory before using it. Let's see the example. public class DemoLocalVariable

{

public void age()

{ int age = 2; //Local variable

age = age + 7; System.out.println("Person age is : " + age);

}

public static void main(String[] args)

{

}

} Output: Person age is 9 2. Instance Variables: Instance variables are non-static variables and declared in a class but outside of any method, constructor, or block. It can be declared in class level before or after use.The instance variables are visible for all methods, constructors and block in the class and default value is depends on the data type of variable. Instance variables can be accessed only by creating objects. Let's see the example.

class Person

{

int age;

String name;

void printName()

{

System.out.println(this.name);

}

public static void main(String args[])

{

Person ob=new Person();

//Instance variable

ob.name="James";

//Calling the method of the class

ob.printName();

}

}


Output: James 3. Static Variables: It is also known as Class Variables.These variables are similarly to instance variables but the difference is that static variables are declared using the static keyword within a class outside of any method, constructor, or block. These variables are created at the start of program execution and destroyed automatically when execution ends. Most static variables are declared as public since they must be available for users of the class. Let's see the example. class Country {

// Static variable

static int countryCounter;

String name;

int tempCounter;

public static void main(String[] args)

{

// Creating first instance

Country ob1 = new Country();

// Assigning values to variables.

ob1.name = "India";

ob1.tempCounter = 1;

++ob1.countryCounter;

// Creating second instance of the class

Country ob2 = new Country();

ob2.name = "france";

ob2.tempCounter = 1;

++ob2.countryCounter;

System.out.println("ob1.countryCounter = " + ob1.countryCounter);

System.out.println("ob2.countryCounter = " + ob2.countryCounter);

System.out.println("Country.countryCounter = " + Country.countryCounter);

}

}


Output: ob1.countryCounter = 2

ob2.countryCounter = 2

Country.countryCounter = 2


9 views0 comments

Recent Posts

See All

Comments


bottom of page