Java variable scope (scope)

This article was written for people who were programming in other languages and would like to program in Java.

What is a scope?

A scope is a usable range. Not limited to variables, the scope is called the scope. This time, I will give an example of the scope of variables. Example 1) スクリーンショット_2020-05-14_11_33_19.png As you can see, you can't bring chain variables directly from main. In other words, the scope of i in chain is in chain, and the scope of i in main is in main. Example 2) スクリーンショット_2020-05-14_11_49_55.png The value declared in the if statement can be used only in the if statement. If you want to use it elsewhere, there is no problem if you change the value declared in the if statement so that it is operated in the if statement. スクリーンショット_2020-05-14_11_50_34.png

Global and local variables

There are two variables, global variables and local variables. Local variables are values (= scope is finite) that can only be used within the range as mentioned above. Global variables, on the other hand, are variables with unlimited scope. Global variables do not exist in Java, but they can be in the same state as global variables. The description method is the description of public static variables. Example) スクリーンショット_2020-05-14_13_07_38.png Global variables can be referenced from anywhere, so you can change their values from other classes. It has a huge impact, so use it only when you need it and use local variables as much as possible.

public and static

I mentioned that global variables are described as public static variables, This is because it extends the scope where both public and static can be used. I will also explain these two points.

Access modifier

public is one of the access modifiers. I will give you one by one. ・ Public has no restrictions. · Protected only allows access from within a class or a subclass that inherits from the class. -If nothing is attached, it can be accessed from all classes in the same package. -Private can only be accessed from inside the class.

Of these, the movement was confirmed except for the protected variable, but since the protected movement was unexpected, it is a condition other than protected once. Confirmation program

practice.java



class Main {
  public static String pubstr = "public";
  protected static String prostr = "protected";
  static String str = "None";
  private static String pristr = "private";
  
  public static void main(String[] args) {
    System.out.println(pubstr); //public
    System.out.println(str); //None
    System.out.println(pristr); //private

    check();
    Sub subclass = new Sub();
    subclass.sub();
  }

  public static void check() {
    System.out.println(pubstr); //public
    System.out.println(str); //None
    System.out.println(pristr); //private
  }
}

class Sub {
  public void sub() {
    Main m = new Main();
    System.out.println(m.pubstr); //public
    System.out.println(m.str); //None
    System.out.println(m.pristr); //Error because private cannot be obtained
  }
}

static static means that you can also use static methods (methods with static). Show a concrete example.

practice.java


class Main {
  public int ni = 10;
  public static int si = 7;
  int di = 999;
  
  public static void main(String[] args) {
    System.out.println(ni); //Error because ni is not a static variable
    System.out.println(si); //Since si is a static variable, 7 is output
  }

  public void check(String[] args) {
    System.out.println(ni); //10 is output because check is not a static method
    System.out.println(si); //7 is output because check is not a static method
  }
}

In this way, only variables with static can be used in methods with static.

Recommended Posts

Java variable scope (scope)
Java variable scope
java array variable
java variable declaration
Java session scope
[Java] Variable name naming memo
About Java variable declaration statements
Pass a variable to Scope.
Scope
Java
Java variable scope (range where variables can be seen)
[Introduction to Java] Variable scope (scope, local variables, instance variables, static variables)
Java
Java Servlet / JSP Request Scope Part 1
Java variable declaration, initialization, and types
[Java] Object-oriented syntax-class / field / method / scope
Java Servlet / JSP Request Scope Part 2
Java environment variable settings (Windows, AdoptOpenJDK11)
How to write Java variable declaration
Assign evaluation result to Java variable
[Basic knowledge of Java] Scope of variables
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
Create variable length binary data in Java
[Java] ArrayDeque
static variable
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
java beginner 4
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
[Java ~ Variable definition, type conversion ~] Study memo
java reflexes
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
[Java] Inheritance
FastScanner Java
java beginner 3
java (encapsulation)
Java inheritance