Java variable scope

Purpose of this article

I'm studying Java, and I'm writing an article to output what I've learned and to establish my knowledge. I will describe what I have learned separately as follows.

-Java variable declaration / initialization / data type (cast and promotion) ・ Type conversion -Variable Scope ・ Character string operation (in preparation) ・ Array operation (in preparation) ・ Operator (in preparation) ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation)

Variable scope

The scope of a variable is the valid range of the defined variable.

Local variables

Variables are basically inaccessible outside the declared location. Variables declared within a method are valid only within that method. This is called a local variable.

test.java


public class Main {
    public static void main(String[] args) {

        int one = 1;

        {
            int two = 2;
        } 
        System.out.println(one + two);
    } 
}

The above program will result in an error. This is because the inner is called outside the scope of the inner. The program will work if you do the following.

test.java


public class Main {
    public static void main(String[] args) {

        int one = 1;
        {
            int two = 2;
            System.out.println(one + two);
        } 
    }
}

3

This is because one is called inside two. One can also be used inside two.

** Variables defined in scope cannot be used if they are out of scope ** Also, as shown below, the if statement has a scope inside the {}, so if it deviates from that, the variables defined in the if statement cannot be accessed.

Main.java


class Main{
  public static void main(String args[]){
    int n = 5;

    if (n == 5) {
      String seikai = "n is 5";
    }
    System.out.println(seikai);
  }
}


Execution result ↓

seikai cannot be resolved to a variable

It can be executed by calling seikai in if as shown below.

Main.java


class Main{
  public static void main(String args[]){
    int n = 5;

    if (n == 5) {
      String seikai = "n is 5";
      System.out.println(seikai);
    }
  }
}

Execution result ↓

n is 5

The same applies to the for statement.

Main.java


class Main{
  public static void main(String args[]) {

    for (int i = 5; i <= 10; i++) {
      System.out.println(i);  //Executable
    }
    System.out.println(i);  //Not executable
  }
}

Execution result ↓

i cannot be resolved to a variable

It can be executed by calling i in for as shown below.

Main.java


class Main{
  public static void main(String args[]) {

    for (int i = 5; i <= 10; i++) {
      System.out.println(i);
    }
  }
}

Execution result ↓

5
6
7
8
9
10

Member variable (field)

In addition to the local variables mentioned above, there are also member variables. Variables declared outside the methods of a class are called member variables (fields). Member variables can be referenced outside the method but within the same class. Also, Java doesn't want to manipulate data directly from the outside, so if you want to reference a variable from another class, you can do this by using modifiers.

`Modifier: Determines where a class and its members are accessible. There are private, protected, public, etc. ``

I will write more about this later.

Recommended Posts

Java variable scope (scope)
Java variable scope
java array variable
Java session scope
Regarding Java variable usage
[Java] Variable name naming memo
variable
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] 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] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
Create variable length binary data in Java
[Java] ArrayDeque
java (override)
java (method)
Java string
java (array)
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
[Java ~ Variable definition, type conversion ~] Study memo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Polymorphism
Studying Java # 0
Java review
Java features
[Java] Inheritance