Alphalearningschool
        

<<< Prev

Next >>>

Up
Variable Creation

Defining, declaring, initializing, reinitializing, and calling/ using a variable

Defining a variable :

Variable creation with value is called defining a variable

 

Syntax:

<Accessibility Modifier> <Modifier> <datatype><variablename> =< value> ;

Ex:

        public static int             x=10;

        public static Example e = new Example ();

Declaring a variable :

Syntax:

<Accessibility Modifier> <Modifier> <datatype><variablename>  ;

Ex:

        public static int    x;         

        public static Example e;  

 

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

 Syntax:

     <Variablename> = <value>   ;

 

 Ex:      //Declaring variables

int p;

Example e;

// assigning a variables

            p = 50;

            e= new Example ();

 

//reassignment

P =70

e = new Example ();

    Calling a variable

  Reading a value from a variable is called calling a variable.

 

  Ex:

        int x = 10;

            //calling x variable for painting

            System.out.prtinln(x);  //10

 

         //calling x variable for initializing y

                int y = x;

Types of Variables

 Based on class scopes variables are divided in two types

 

  1. Local Variables (parameters): variables created inside a method (or) block are called local variables
  2. 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

Definitions

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.

class Example {

	// static variables public static void main(String() args)

	{

		static int a = 10; // local variables

		static int b = 20;
		int p = 50;

		// non-static variables int q = 60;

		int x = 30;
	}

	int y = 40;
}

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