Variable, final constant, class member variable, static keyword, instance variable, static variable, local variable

Source: Internet
Author: User

1. Constants: Also known as final variables, can only be assigned once in the entire program

final int num = 1215;

num = 1216; //error, can only be assigned once

2. Class member variable: A variable that is not defined in a method is a member variable, and a variable defined in the method body is a local variable

Member variable meaning: object's properties

public class book{

int id;

String name; //Two member variables

Public String GetName () {

int id = 0; //Local variables, must be assigned or initialized

}

}

3.static keyword: A variable declared as static, constants and methods are statically members.

For static members, all objects share a variable, and you can use the class name. Static member in this class or other class to invoke a static member.

In a member variable, there is a static member variable, and no static adornment is an instance variable (access needs to create an object).

Pubic class statictest{

static int i = 5; //static member variable

public static void Main () {

System.out.println (STATICTEST.I); //Call static member variable

}

}

NOTE: You cannot declare a local variable in a method body as static

The This keyword cannot be used in static methods, and non-static methods cannot be called directly

(distinguishes between classes and objects: this represents a reference to the object that called the function, and the static method belongs to the class, not to the object, and the object does not necessarily exist after the static method is successfully loaded).

4. Local variables can override member variables (global variables) when they are named repeatedly with member variables, except for local variables in the loop body (distinguish the scope of each variable)

Variable, final constant, class member variable, static keyword, instance variable, static variable, local variable

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.