Java variable types and their instances

  • 2020-06-23 00:28:28
  • OfStack

In Java, all variables must be declared before they are used. The basic format for declaring variables is as follows:

type identifier [ = value][, identifier [= value] ...] ;

Format description: type is the Java data type. identifier is the variable name. Multiple variables of the same type can be declared using commas.

The following lists 1 declaration instances of variables. Note that some contain initialization procedures.


int a, b, c;     //  The statement 3 a int Type integer: a ,  b , c . 
int d = 3, e, f = 5; // d The statement 3 An integer with an initial value. 
byte z = 22;     //  Declare and initialize z . 
double pi = 3.14159; //  The statement pi . 
char x = 'x';    //  variable x The value of is a character 'x' . 

The types of variables supported by the Java language are:

A local variable

Member variables

Class variables

Java local variable

A local variable is declared in a method, constructor, or statement block;

Local variables are created when a method, constructor, or block of statements is executed. Variables are destroyed when they are executed.

Access modifiers cannot be used on local variables;

A local variable is only visible in the method, constructor, or block of statements that declare it;

Local variables are allocated on the stack.

Local variables have no default values, so local variables must be initialized before they can be used.

Example 1

age is a local variable in the following example. Defined in the pubAge() method, its scope is limited to that method.


public class Test{ 
  public void pupAge(){
   int age = 0;
   age = age + 7;
   System.out.println("Puppy age is : " + age);
  }
  
  public static void main(String args[]){
   Test test = new Test();
   test.pupAge();
  }
}

The compilation and operation results of the above instances are as follows:

Puppy age is: 7

Example 2

The age variable is not initialized in the following example, so an error occurs at compile time.


public class Test{ 
  public void pupAge(){
   int age;
   age = age + 7;
   System.out.println("Puppy age is : " + age);
  }
  
  public static void main(String args[]){
   Test test = new Test();
   test.pupAge();
  }
}

The compilation and operation results of the above instances are as follows:


Test.java:4:variable number might not have been initialized
age = age + 7;
     ^
1 error

The instance variables

Instance variables are declared in a class but outside of methods, constructors, and statement blocks;

When an object is instantiated, the value of each instance variable is determined.

Instance variables are created when an object is created and destroyed when it is destroyed.

The value of an instance variable should be referenced by at least one method, constructor, or block of statements so that instance variable information can be obtained externally.

Instance variables can be declared before or after use;

Access modifiers can modify instance variables;

Instance variables are visible to methods, constructors, or blocks of statements in a class. In general, instance variables should be set to private. Can be made by using the access modifier

Instance variables are visible to subclasses;

Instance variables have default values. The default value for numeric variables is 0, for Boolean variables is false, and for reference type variables is null. The value of the variable can be

Specified when declared or in a constructor;

Instance variables can be accessed directly by variable name. But in static methods and other classes, you should use the fully qualified name ObejectReference.VariableName.

Example:


import java.io.*;
public class Employee{
  //  This member variable is visible to subclasses 
  public String name;
  //  Private variables, visible only in this class 
  private double salary;
  // Yes in the constructor name The assignment 
  public Employee (String empName){
   name = empName;
  }
  // set salary The value of the 
  public void setSalary(double empSal){
   salary = empSal;
  } 
  //  The print information 
  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();
  }
}

The compilation and operation results of the above instances are as follows:


name : Ransika
salary :1000.0

Class variables (static variables)

Class variables, also known as static variables, are declared in the class with the static keyword, but must be outside the method constructor and statement block.

No matter how many objects a class creates, a class has only one copy of its variables.

Static variables are rarely used except when declared as constants. Constants are variables declared of type publc/private, final, and static. Constant cannot be changed after initialization.

Static variables are stored in static storage. Often declared as constants, static is rarely used alone to declare variables.

Static variables are created at the beginning of the program and destroyed at the end.

Have similar visibility to instance variables. But to be visible to the consumer of the class, most static variables are declared of type public.

Default values are similar to instance variables. Numeric variables default to 0, Boolean to false, and reference to null. The value of a variable can be specified at declaration time or in a constructor. In addition, static variables can be initialized in static statement blocks.

Static variables can be accessed as: ClassName.VariableName.

When a class variable is declared as type public static final, the name of the class variable must be in uppercase. If static variables are not of TYPE public and final, they are named the same way instance variables and local variables are named 1.

Example:


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

The compilation and operation results of the above instances are as follows:

Development average salary:1000

Note: If other classes want to access the variable, they can do so as Employee.DEPARTMENT.

I hope this article can be helpful to you


Related articles: