JavaSE-basic-5-variable, constant, package mechanism (version1)

variable

What are variables? In short, it is the amount that can be changed.

type varname [ = value][, varname [ = value] ...] ;
Data type variable name = variable value.

Three elements of variables:
1. Data type: determines the space allocated in memory
2. Variable name: space alias
3. Value: data stored in the space

Attention

  • Each variable must declare its type before use, and the declaration must end with a semicolon. (Java is a strongly typed language, which requires that the data type stored in the variable must be consistent with the type declared by the variable.).
  • Variable types can be basic types. It can also be a reference type.
  • Variable name must be a legal identifier.

Preheating diagram:

Member variable

The scope is the whole class, which is equivalent to the global variable in C. It is defined outside the method body and statement block, and is generally defined under the declaration of the class; Member variables include instance variables and static variables (class variables);

Instance variable

Independent of variables other than methods and without static modification, they are declared in a class, but outside methods, construction methods and statement blocks, the default value of numerical variables is 0, the default value of Boolean variables is false, and the default value of reference types is null;

Class / static variable

Variables independent of methods are decorated with static. The default value is similar to the instance variable. There is only one in a class, which belongs to the common object. It is stored in the static storage area and is often declared as a constant. The call is generally the class name, static variable name, or object name and static variable name;

local variable

The access modifier of the variable in the method of class cannot be used for local variables. It is declared in the method, constructor or statement block and allocated on the stack. It has no default value and must be initialized;

package com.cunyu.demo

public class Demo {

    private String name;  //Member variable, instance variable
    private int age;  //Member variable, instance variable
    private int ID;  //Member variable, instance variable

    public static final String school = "Kassel College";  //Member variable, static variable (class variable)
    public static String level = "SSS";    //Member variable, static variable (class variable)

    public int getAge() {
        return age;
    }

    public int getId() {
        return ID;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setId(int ID) {
        this.ID = ID;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void study(){
            String subject1 = "Tu Long";  //local variable
            String subject2 = "alchemy"; //local variable
            System.out.println("Study subjects: " + subject1 + "," + subject2);
        }

    public static void main(String[] args) {
        Demo demo = new Demo();
        demo.setAge(23);
        demo.setId(14000001);
        demo.setName("Chu Zihang");
        System.out.println("ID: " + demo.getId() + "Age: " + demo.getAge() + "Name: " + demo.getName());
        System.out.print("Major subjects: ");
        demo.study();
        System.out.println("College:" + Demo.school);
        System.out.println("Grade:" + Demo.level);
    }
}

Difference between instance variable and local variable:

1) Scope
Instance variable: valid for the entire class.
Local variable: valid only in a certain range. (generally refers to the method, in the statement body)
(2) Storage location
Instance variable: exists with the creation of the object, disappears with the disappearance of the object, and is stored in heap memory.
Local variable: it exists when a method is called or a statement is executed and is stored in stack memory. When the method is called or the statement ends, it is automatically released.
(3) Initial value
Instance variable: has a default initial value.
Local variable: there is no default initial value, which must be assigned before use.

Static variable (class variable) & difference between instance variables:

A: Call mode
Static variables, also known as class variables, can be called directly through the class name. It can also be called by object name. This variable belongs to class.
Instance variable, which can only be called by object name. This variable belongs to the object.
B: Storage location
Static variables are stored in the static area of the method area.
Instance variables are stored in heap memory.
C: Life cycle
Static variables exist with the loading of the class and disappear with the disappearance of the class. Long life cycle.
Instance variables exist with the creation of objects and disappear with the disappearance of objects.
D: Relevance to objects
Static variables are common to all objects. One object changes its value, and the other objects get the changed result;
Instance variables are private to objects. If an object changes its value, it will not affect other objects;

public class SE004_LeiBianLiangAndShiLiBianLiang {
	public static void main(String[] args) {
		Demo d1 = new Demo();
		Demo d2 = new Demo();
	}
	
}
 
class Demo{
	static int i = 1;	//Class variable (static variable)
	int j = 1;		//Instance variable
	public Demo(){
		i++;
		j++;
		System.out.println(i+":"+j);
	}
}

Operation results

2:2
3:2

constant

Constant: a value that cannot be changed after initialization. That is, the value that will not change.
Constant names generally use uppercase characters
Syntax: final constant name = value; For example: final String LOVE = 'java';

package

In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names.
The syntax format of package statement is: package pk1.pk2[.pk3[.pk4]];
Generally, the company domain name is inverted as the package name: www.baidu.com -- > com.baidu.www

In order to use the members of a package, you need to import the package in Java
Syntax: import package name;

javaDoc

Javadoc is used to describe the role of classes or methods. Javadoc can be written on classes and methods.

Common labels

@Author identifies the author of a class
@Version specifies the version of the class
@deprecated refers to an expired class or member
@Throws throws an exception
@param parameters
@Return return value


Generate JavaDoc documents with idea. Click go

If there are any errors / questions, please correct / leave a message!
References / materials:
Baidu Encyclopedia
Static variables (class variables), instance variables, local variables and member variables in Java
Differences among instance variables, local variables and static variables (class variables)
Differences between member variables and instance variables in JAVA
Definition and initialization of Java variables and constants

Tags: Java

Posted on Sat, 18 Sep 2021 12:19:53 -0400 by literom