Variable Types.

Local Variables.

  • Local variables we can declare inside the methods, constructors, or blocks.
  • After creating, variable will destroyed once it exits the method, constructor, or block.
  • Access modifiers cannot use for local variables.
  • Local variables are visible only within the declared method, constructor, or block.
  • Local variables we can implement at stack level internally.
  • There is no default value for local variables, so local variables should declared, and an initial value should assigned before the first use.
public class Javavariable {
   public void Age() {
      int age = 0;
      age = age + 8;
      System.out.println("Puppy age is : " + age);
   }

   public static void main(String args[]) {
      Javavariable j = new Javavariable();
      j.Age();
   }
}

Instance Variables.

  • Instance variables we can declare in a class, but outside a method, constructor or any block.
  • When a space is allocating for an object in the heap, a slot for each instance variable value to create.
  • Instance variables are creating when an object created with the use of the keyword ‘new’ and destroy when the object is destroying.
  • variables hold values that must 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 we can declare in class level before or after use.
  • Access modifiers can give for instance variables.
  • The instance variables are visible for all methods, constructors and block in the class. Normally, it is recommending to make these variables private (access level). However, visibility for subclasses can give for these variables with the use of access modifiers.
  • Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can assigned during the declaration or within the constructor.
  • Instance variables we can access directly by calling the variable name inside the class. However, within static methods (when instance variables are giving accessibility), they should be called using the fully qualified name. ObjectReference.VariableName.
•	import java.io.*;
•	public class Employee {
•	
•	   // this instance variable is visible for any child class.
•	   public String name;
•	
•	   // salary  variable is visible in Employee class only.
•	   private double salary;
•	
•	   // The name variable is assigned in the constructor.
•	   public Employee (String empName) {
•	      name = empName;
•	   }
•	
•	   // The salary variable is assigned a value.
•	   public void setSalary(double empSal) {
•	      salary = empSal;
•	   }
•	
•	   // This method prints the employee details.
•	   public void printEmp() {
•	      System.out.println("name  : " + name );
•	      System.out.println("salary :" + salary);
•	   }
•	
•	   public static void main(String args[]) {
•	      Employee empOne = new Employee("Ransika");
•	      empOne.setSalary(1000);
•	      empOne.printEmp();
•	   }
•	}

Class/Static Variables.

  • Class variables also known as static variables and declare with the static keyword in a class, but outside a method, constructor or a block.
  • There would only one copy of each class variable per class, regardless of how many objects are creating from it.
  • Static variables are rarely used other than being declared as constants. Constants are variables that we can declare as public/private, final, and static. Constant variables never change from their initial value.
  • Static variables we can store in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.
  • Static variables we can create d when the program starts and destroyed when the program stops.
  • Visibility is similar to instance variables. However, most static variables can declare 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 Booleans, it is false; and for object references, it is null. Values can assigned during the declaration or within the constructor. Additionally, values can assigned in special static initializer blocks.
  • Static variables can accessed by calling with the class name ClassName.VariableName.
  • When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.
•	import java.io.*;
•	public class Employee {
•	
•	   // salary  variable is a private static variable
•	   private static double salary;
•	
•	   // DEPARTMENT is a constant
•	   public static final String DEPARTMENT = "Development ";
•	
•	   public static void main(String args[]) {
•	      salary = 1000;
•	      System.out.println(DEPARTMENT + "average salary:" + salary);
•	   }
•	}
Variable Types

Java Variables.

Leave a Reply

Your email address will not be published. Required fields are marked *