Defining, declaring, initializing, reinitializing, and calling/ using a variable
Defining a variable :
Variable creation with value is called defining a variable
<Accessibility Modifier> <Modifier> <datatype><variablename> =< value> ;
public static int x=10;
public static Example e = new Example ();
Declaring a variable :
<Accessibility Modifier> <Modifier> <datatype><variablename> ;
public static int x;
public static Example e;
Exercise programs to be solved
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative3
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
Assigning and reassigning a variable
Storing a value in a variable at the time of its creation is called initializing a variable
Storing a value in a variable after its creation is called assigning a value to a variable
Types of Variables
Based on class scopes variables are divided in two types
- Local Variables (parameters): variables created inside a method (or) block are called local variables
- Class Level Variables: variables created at class level are called class level variables
Types of class level variables
Class level variables are divided into two types, based on the time they are getting memory location. They are
- Static variables
- Non-static variables
The class level variable which has static keyword in its creation statement is called static variable, else it is called non –static variable.
Memory location of all above three variables
- Local variables get memory location when method is called and their creation statement is executed. They get memory with respect to method, so they are also called method variables. Local variables are automatically created when method is executing and are destroyed automatically after method execution is completed, so they are also called auto variables.
- Static variables get memory location when class is loaded into JVM. They get memory with respect to class name, so they are also called “class variables” also called “Fields”.
- Non-static variables get memory location when object is created using new keyword. They get memory with respect to object, so they are also called “object variables or instance variables or properties or attributes “ are also called “Fields”
Below diagram shows different scopes in class and all above three variables creation.
Local variables and its rules
Local variables and its rules
Rule #1: local variable cannot be accessed from another method. Because its scope is restricted only within its method, also we cannot guarantee variable creation, because that method may or may not be called. It leads to CE: “cannot find symbol”.
Rule #2: local variable should not be accessed without initialization.
It leads to CE: ”variable might not have been initialized”.
Rule #3: local variable must be accessed only after its creation statement; because method execution is sequential execution from top to bottom. If we access it before its creation statement it leads to CE: “cannot find symbol”.
Class level variables and its rules
Rule: Non-static variables and methods must be accessed with object from static methods; else it leads to above CE: non-static variable cannot be referenced from static context, because non static variable and non-static method does not get memory location directly at the time of class loading.
Final variables: The class level or local variable that has fin al keyword in its definition is called final variable.
Rule: once it is initialized by developer its value cannot be changed. If we try to change its value it leads to compile time error.
Transient variables: The class variable that has transient keyword in its definition is called transient variable.
Rule: local variable cannot be declared as transient. It leads to CE. Illegal start expression
Note: We declare variable as transient to tell JVM that we do not want to store variable value in a file in object serialization. Since local variable is not part object, declaring it as transient is illegal.
Volatile variable: The class level variable that has volatile keyword in its definition is called volatile variable.
Rule: local variable cannot be declared as volatile .It leads to CE: illegal start expression