Java Basics-Variables are you out of your coma?

Source: Internet
Author: User

Tag: style static tracking post ACK member time static class

variables in Java are broadly divided into two broad categories: member variables and local variables.

member variables:
variables defined inside the class body are called member variables.
assume that the member variable has a static keyword adornment. The member variable is called the Static Variables or class Variables.
assuming that the member variable does not have a static keyword adornment, the member variable is called non-static variables or instance Variable.

Local Variables:
Parameters defined within the method, variables defined in the code block, are local variables.

class variables ( static variables )
1. Ability to forward reference
2. Variables belong to the class itself
3. Class variables do not depend on instances of classes, and class variables are allocated only once in the stack memory at initialization time. No matter how many instances of the class are created. No longer allocates space for class variables
4. Access class variables by using an arbitrary instance of the class, and the underlying will turn it into a class variable through the class itself. They have the same effect.
5. Once the value of the class variable has been changed, access the class variable through a random instance of the class or class, and the resulting value will be changed .
6. Will initialize before initialization of the class

instance variable (non-static variable)
1. Cannot forward reference, assuming forward reference, is called illegal forward reference, this is not agreed
2. The variable belongs to the instance object of the class
3. Allocating memory space as instances of classes are created

non-static code block
code that is wrapped directly by {}. Called non-static code block

Static code block
code that is wrapped directly by static {}, called a static code block

initialization time for class variables (static variables), instance variables (non-static variables), static code blocks, non-static blocks of code
A static keyword, such as a class variable [static variable], static code block, is initialized before the class is initialized to create an instance object, and is sequentially run from top to bottom.
initialization without static keyword modifiers (e.g., instance variables [non-static variables], non-static blocks of code) is actually extracted into the constructor of the class and is run, but it is compared to the class constructor
code blocks first run to. It is also run sequentially from top to bottom.

Java Basics-Variables are you out of your coma?

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.