Java variable type

Source: Internet
Author: User

Java variable type

  • Local variable
  • Member variables
  • Class variable
Java local variables
  • Local variables are declared in methods, constructor methods, or statement blocks;
  • Local variables are created when methods, constructor methods, or statement blocks are executed. After they are executed, the variables are destroyed;
  • The access modifier cannot be used for local variables;
  • Local variables are only visible in the declared methods, constructor methods, or statement blocks;
  • The local variable is inStack allocated.
  • The local variable does not have the default value. Therefore, after the local variable is declared, it must be initialized before it can be used..
  • Instance variables
    • Instance variables are declared in a class, but outside of methods, constructor methods, and statement blocks;
    • After an object is instantiated, the value of each instance variable is determined;
    • Instance variables are created when an object is created and destroyed when the object is destroyed;
    • The instance variable value should be referenced by at least one method, constructor, or statement block, so that the external can obtain instance variable information through these methods;
    • Instance variables can be declared before or after use;
    • The access modifier can modify instance variables;
    • Instance variables are visible to methods, constructor methods, or statement blocks in the class. Generally, the instance variables should be set to private. You can use an access modifier to make instance variables visible to sub-classes;
    • Instance variables have default values. The default value of the numeric variable is 0, the default value of the Boolean variable is false, and the default value of the referenced type variable is null. Variable values can be specified during Declaration or in constructor;
    • Instance variables can be accessed directly through the variable name. However, in static methods and other classes, you should use the fully qualified name: ObejectReference. VariableName.
    • Instance:

      Import java. io. *; public class Employee {// This member variable is visible to the subclass public String name; // private variable, only visible to the class private double salary; // assign the value of public Employee (String empName) {name = empName;} to name in the constructor. // set the value of salary to public void setSalary (double empSal) {salary = empSal ;}
    • Class variable (static variable)
      • A class variable is also called a static variable. It is declared with the static keyword in the class,It must be out of the method constructor and statement block..
      • No matter how many objects a class creates, the class only has a copy of the class variables.
      • Static variables are rarely used except declared as constants. Constants are variables declared as public/private, final, and static. Constants cannot be changed after initialization.
      • Static variables are stored in the static storage area. It is often declared as a constant and rarely declares variables independently using static.
      • Static variables are created at the beginning of the program and destroyed at the end of the program.
      • Similar to instance variables. To be visible to users of the class, most static variables are declared as public.
      • The default value is similar to the instance variable. The default value of the numeric variable is 0, the default value of the Boolean variable is false, and the default value of the reference type is null. The variable value can be specified during Declaration or in the constructor. In addition, static variables can be initialized in static statement blocks.
      • You can use the following static variables:ClassName. VariableName.
      • When a class variable is declared as public static final, the class variable name must use uppercase letters. If the static variables are not of the public and final types, they are named in the same way as the instance variables and local variables.

      Instance:

      Import java. io. *; public class Employee {// salary is a static private variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Developer "; public static void main (String args []) {salary = 10000; System. out. println (DEPARTMENT + "Average salary:" + salary );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.