java basic knowledge 3

Java variable types

Java local variables

  • Local variables declared in the method, constructor or statement blocks;
  • Local variables when the method, constructor, or block of statements to be executed to create, when they are executed, the variable will be destroyed;
  • Modifier can not be used to access local variables;
  • Local variables are declared in the method of its, or a statement block constructor visible;
  • Local variables are allocated on the stack.
  • There is no default local variables, so the local variable is declared, must be initialized before they can be used.
package com.runoob.test;
 
public class Test{ 
   public void pupAge(){
      int age = 0;
      age = age + 7;
      System.out.println("小狗的年龄是: " + age);
   }
   
   public static void main(String[] args){
      Test test = new Test();
      test.pupAge();
   }
}

operation result:

Puppy age: 7

Instance variables

  • Variable is declared in a class, but in the method, constructor, and outside block of statements;
  • When an object is instantiated, each value instance variable is determined to follow;
  • In the instance variables created when the object is created, destroyed when the object is destroyed;
  • Instance variable's value should be at least a reference method, constructor, or block of statements, so that the external information can be acquired by these means instance variables;
  • Examples of variables can be declared before use or after use;
  • Access modifier can be modified instance variables;
  • For instance variable in the class method, constructor, or block of statements is visible. Under normal circumstances should the instance variables set to private. Examples of variables can be made visible by using a subclass access modifiers;
  • Instance variables have a default value. Default numeric variable is 0, the default value of the Boolean variable is false, the default reference value type variable is null. Values ​​of the variables can be specified in the declaration, it can be specified in the constructor;
  • Examples of variables can be accessed directly via the variable name. However, static methods and other classes, you should use the fully qualified name: ObejectReference.VariableName.
import java.io.*;
public class Employee{
   // 这个实例变量对子类可见
   public String name;
   // 私有变量,仅在该类可见
   private double salary;
   //在构造器中对name赋值
   public Employee (String empName){
      name = empName;
   }
   //设定salary的值
   public void setSalary(double empSal){
      salary = empSal;
   }  
   // 打印信息
   public void printEmp(){
      System.out.println("名字 : " + name );
      System.out.println("薪水 : " + salary);
   }
 
   public static void main(String[] args){
      Employee empOne = new Employee("RUNOOB");
      empOne.setSalary(1000.0);
      empOne.printEmp();
   }
}

The results compiled to run above examples are as follows:
Name: RUNOOB
Salary: 1000.0
class variables (static variables)

  • Class variables, also known as static variables in the class with the static keyword statement, but it must be in addition to the methods.
  • Whether a class creates a number of objects, classes have only one copy of the class variable.
  • In addition to static variable is declared as a constant outer rarely used. Constants are declared as public / private, final and static types of variables. Constants can not be changed after initialization.
  • Static variables are stored in static memory. Often declared as constant, rarely use static variables declared separately.
  • Static variables are created when first accessed, destroyed at the end of the program.
  • Example visibility similar variables. However, to the user of the class can be seen, most of the static type variable is declared as public.
  • Default instance variables and the like. Numerical variables default value is 0, the default value is Boolean false, the default value is a reference type null. Value of a variable can be specified at the time of declaration, it can also be specified in the constructor. Further, static variables can also initialize static statement block.
  • Static variables can be: ClassName.VariableName of access.
  • When the class variable is declared as public static final type, class variable names it is generally recommended to use capital letters. And if not public static final variable type, which is consistent naming named instance variables and the local variables.
    example:
 import java.io.*;
 
public class Employee {
    //salary是静态的私有变量
    private static double salary;
    // DEPARTMENT是一个常量
    public static final String DEPARTMENT = "开发人员";
    public static void main(String[] args){
    salary = 10000;
        System.out.println(DEPARTMENT+"平均工资:"+salary);
    }
}

The above examples compiled results are as follows:

Developers average wage: 10000.0

Member variables Local variables Static variables
Defined position In class, the outer Method Formal parameter method, or a method In class, the outer Method
Initialization value There are default initialization value No, define, can be used after the assignment There are default initialization value
Called Object calls - Object calls, the class name calling
storage location Heap Stack Methods district
Life cycle Live and die with the object And methods perish Live and die with the class
Aliases Instance variables Class variables
Published 49 original articles · won praise 7 · views 3083

Guess you like

Origin blog.csdn.net/qq_44797965/article/details/104097442