Tutorials

Java Variable Interview Questions


Java Variable Interview Questions 1


What is variable in java?

Variable is like a container that is use to hold value of different data type.

You need to specify the data type of the variable while declaring it to make it clear what kind of value it will be storing.

 

To use a variable, a programmar needs to:

  • declare a variable
  • initialize the variable
  • then use it

For example. if we want to store a int value:

public int num;  //declare variable num

num=11;  //initialize value to variable num

 

We have discussed in details about variable, declaration, initialization and different types of variable in details here: Click to read


How many types of variable are there?

We have following types of variable available in java:

  • local variables
  • instance variable 
  • static variable
  • method parameters

To learn in detail about each of them with examples click here

You can study answers about the next three questions following the same link.


What is local variable?

A variable declared inside method, block or loop is known as local variable.

Their scope or visibility is local to the entity in which they are declared. They cannot be used outside these entity.

 

A local variable should be initialized before use otherwise compiler error will occur.

 

Click here to read more about local variable with example


What is static variable?

Static variables are also referred to as class variable as they belongs to a class.

We can declare a static variable using static keywords.

Static variable is shared among different instances of a class. So static variables requires less storage memory space than instance variables.

 

Click here to read more about static variable with example


What is instance variable?

An instance variable is used by objects to store their states.

Each instance or object will have separate value which cannot be shared by other instances.

 

If value is not assigned for default variable then default value is assigned to it during run time.

 

Click here to read more about instance variable with example

 



Please Share this page
Views : 119
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!