Play AlgoQuiz

Rate Us :

Share with Friends :


Types of variables in Java :

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 data types. 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 includes initialization.

Example :

int a, b, c; // declares three int type a, b, c int d = 3, e, f = 5; // declares three more int type d, e, f : initializing d, f. byte b = 5; // initializes b of byte type double pi = 3.14159; // declares an approximation of pi of double type char c = 'a'; // the variable c has the value 'a' which is of char type

There are three kinds of variables in Java :

  • Local variables

  • Instance variables

  • Class / static variables

Local variables :

  • Local variables are declared in methods, constructors or blocks.

  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.

  • Access modifiers cannot be used for local variables.

  • Local variables are visible only within the declared method, constructor or block.

  • Local variables are implemented at stack level internally.

  • There is no default value for local variables so local variables should be declared and an initial value should be assigned before using it.

Example :

class Example { public static void main(String [ ] args) { Example example = new Example(); example.age_method(); } public void age_method() { int age = 0; age = age + 18; System.out.println("Age is : " + age); } }

Output :

Age is : 18

Here, age is a local variable. This is defined inside age_method() method and it's scope is limited to this method only.

Example :

class Example { public static void main(String [ ] args) { Example example =new Example(); example.age_method(); } public void age_method() { int age; age = age + 10; System.out.println("Age is : " + age); } }

Output :

Compile time error : variable age might not have been initialized

Instance variables :

  • Instance variables are declared in a class, but declared outside a method, constructor or any block.

  • When a space is allocated for an object in the heap, a slot for each instance variable's value is created.

  • Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

  • Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object’s state that must be present throughout the class.

  • Instance variables can be declared in class level before or after use.

  • Access modifiers can be given for instance variables.

  • The instance variables are visible for all methods, constructors and blocks in the class. Normally, it is recommended to make these variables private (access level).However visibility for subclasses can be given for these variables with the use of access modifiers.

  • Instance variables have default values. For numbers, the default value is 0; for boolean, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor.

  • Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class (when instance variables are given accessibility) should be called using the fully qualified name - object_reference.variable_name

Example :

class Example { public String name; // The name variable is assigned in the constructor. private double salary; // The salary variable is visible in Example class. only. public void example(String empName) { name = empName; } // The name variable is assigned a value. public void setSalary(double empSal) { salary = empSal; } public void printEmp() { System.out.println("Name : " + name ); System.out.println("Salary : " + salary); } // This method prints the employee details. public static void main(String [ ] args) { Example empOne = new Example(); empOne.example("Algo"); empOne.setSalary(1000); empOne.printEmp(); } }

Output :

Name : Algo Salary : 1000.0

Class / static variables :

  • Class variables are also known as static variables which are declared with the static keyword in a class, but declared outside a method, constructor or a block.

  • There would be only one copy of each class variable per class, regardless of how many objects are created from it.

  • Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public / private, final and static. Constant variables never change from their initial value.

  • Static variables are stored in static memory.

  • Static variables are created when the program starts and destroyed when the program stops.

  • Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.

  • Default values are same as instance variables. For numbers, the default value is 0; for boolean, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks.

  • Static variables can be accessed by calling with the class name - class_name.variable_name

  • When declaring class variables as public static final, then variables names (constants) are all in upper case. If the static variables are not public and final then the naming syntax is the same as instance and local variables.

Example :

import java.io.*; class Example { private static double salary; // salary variable is a private static variable public static final String DEPARTMENT = "Development"; // DEPARTMENT is a constant public static void main(String [ ] args) { salary = 1000; System.out.println(DEPARTMENT + " average salary : " + salary); } }

Output :

Development average salary : 1000.0
Note :

If the variables are access from outside the class then constant should be accessed as : Example.DEPARTMENT


MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.