0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Java_ブロックにおける変数のスコープについて

0
Last updated at Posted at 2023-03-14

paizaラーニングforTEAM Java入門編
レッスン6の#06

6-#06
// ブロックのスコープを理解しよう
// ブロック内で宣言された変数はブロックの外側では使えない

public class Main {
    public static void main(String[] args) {
        System.out.println("hello world");
        
        int num = 0; // ifブロック外で変数numを宣言
        if (num == 0) {
            String msg = "paiza"; // ifブロック内で変数msgを宣言
            System.out.println(msg + num); // 変数msgと変数numはifブロック内で使用できる
        }
        // System.out.println(msg); // 変数msgはifブロック外では使用できない
        System.out.println(num); // 変数numはifブロック外でも使用できる
        
        for (int i = 1; i < 5; i++) { // forブロック外で変数iを宣言
            String msg = "Java"; // forブロック内で変数msgを宣言_ifブロック内の変数msgとは独立している
            System.out.println(msg + i); // 変数msgと変数iはforブロック内で使用できる
        }
        // System.out.println(msg); // 変数msgはforブロック外では使用できない
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?