Scope of the 6.Java variable

Source: Internet
Author: User

In Java, the scope of a variable is divided into four levels: class-level, object-instance-level, method-level, block-level.

Class-level variables, also known as global-level variables or static variables, need to be decorated with the static keyword, which you can learn from in contrast to static variables in C + +. Class-level variables already exist after the class is defined, occupy memory space, can be accessed through the class name, and do not need to be instantiated.

An object instance-level variable is a member variable that is not allocated until the memory space is instantiated for access.

A method-level variable is a variable defined inside a method, which is a local variable.

A block-level variable is a variable that is defined inside a block, the lifetime of the variable is the block, and the block disappears, such as the IF and for statements. A block is a code surrounded by curly braces, for example:

  1. {
  2. int Age = 3;
  3. String Name = "Www.weixueyuan.net";
  4. //correct, the age and name variables can be accessed inside the block
  5. System. Out. println(name + "already" + Age + "old");
  6. }
  7. Error, the age and name variables cannot be accessed outside the block
  8. System. Out. println(name + "already" + Age + "old");


Description

    • In addition to accessing the method-level variables inside the method, you can access the class-level and instance-level variables.
    • The inside of a block can access class-level, instance-level variables, and if the block is contained inside a method, it can also access the method-level variables.
    • Method-level and block-level variables must be initialized to be displayed, otherwise they cannot be accessed.


Demo Code:

  1. Public class Demo{
  2. public static String name = "Micro School"; //class-level variables
  3. public int i; //Object instance-level variables
  4. //attribute block, run when class initializes properties
  5. {
  6. int J = 2; Block-level variables
  7. }
  8. public void test1() {
  9. int J = 3; //Method-level variables
  10. if(j = = 3) {
  11. int k = 5; //block-level variables
  12. }
  13. //Cannot access block-level variables, block-level variables can only be accessed within the block
  14. System. Out. println("name=" + name + ", i=" + I + ", j=" + j);
  15. }
  16. public static void main(String[] args) {
  17. //Do not create objects, access class-level variables directly from the class name
  18. System. Out. println(Demo. Name);
  19. //How to create an object and access it
  20. Demo T = New demo();
  21. T. Test1();
  22. }
  23. }

Operation Result:
Micro Science Court
Name= Micro School, I=0, j=3

Scope of the 6.Java 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.