Java中的“作用域"是什么? [英] What is 'scope' in Java?

查看:352
本文介绍了Java中的“作用域"是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始编码.我想对同一变量使用一次switch语句两次,并被告知要执行此操作,变量必须在范围内".

I just started coding. I want to use a switch statement twice for the same variable, and I was told that to do this the variable would have to be 'in scope'.

作为一个初学者,我不知道那意味着什么.那么,范围是什么意思呢?而且,如果变量不在范围内,我该如何在范围内进行设置?

Being a beginner, I have no idea what that means. So what does being in scope mean? And, if a variable isn't in scope, how do I make it in scope?

推荐答案

如果代码可以访问,则局部变量 1 在范围内,如果不能访问,则不在范围内.在Java中,变量的作用域是声明它们的块({}).因此:

A local variable1 is "in scope" if code can access it and out of scope if it can't. In Java, variables are scoped to the block ({}) they're declared in. So:

void foo() {
    int a = 42;

    if (/*some condition*/) {
        String q = "Life, the Universe, and Everything";

        // 1. Both `a` and `q` are in scope here
        System.out.println(a);
        System.out.println(q);
        if (/*another condition*/) {
            // 2. Both `a` and `q` are in scope here, too
            System.out.println(a);
            System.out.println(q);
        }
    }

    // 3. Only `a` is in scope here
    System.out.println(a);
    System.out.println(q); // ERROR, `q` is not in scope
}

注意上面的(1),(2)和(3):

Note (1), (2), and (3) above:

  1. 该代码可以访问q,因为q与该代码位于同一块中; tt可以访问a,因为它是在包含块中声明的.

  1. The code can access q because q is declared in the same block as the code; tt can access a because it's declared in the containing block.

代码可以访问q,因为它是在包含块中声明的;它可以访问a,因为它位于下一个阻止区域中.

The code can access q because it's declared in the containing block; it can access a because it's in the next block out.

该代码可以访问a,但不能访问q,因为q没有在该块或包含该块的任何块(或其他块)中声明. >

The code can access a, but not q, because q isn't declared in the block or any of the blocks (or a couple of other things) containing it.

弄清楚什么是不合格的标识符(例如上面的aq,而不是合格的this.foo中的fooq.toLowerCase中的toLowerCase))是,Java编译器将依次查找每个位置,直到找到匹配的位置:

When figuring out what an unqualified identifier (like a or q above, as opposed to the foo in this.foo or the toLowerCase in q.toLowerCase, which are qualified) is, the Java compiler will look in each of these places, one after the other, until it finds a match:

  • 对于最里面的块中具有该名称的变量
  • 对于下一个屏蔽区中具有该名称的变量,依此类推
  • 对于在当前类中具有该名称的 field 2 方法(通常为 member )
  • 对于已导入包中具有该名称的类
  • 对于具有该名称的包裹
  • For a variable with that name in the innermost block
  • For a variable with that name in the next block out, and so on
  • For a field2 or method (generally: member) with that name in the current class
  • For a class with that name from a package that's been imported
  • For a package with that name

该列表还有其他一些(我不会和初学者一起进入静态导入).

There are a few others for that list (I'm not going to get into static imports with a beginner).

还有很多其他方面,我建议您完成一些教程和/或一本Java入门书,以了解更多信息.

There's a lot more to scope, I suggest working through some tutorials and/or a beginning Java book for more.

1 局部变量"与变量"- Java语言规范使用变量"的方式比大多数人在普通语言中使用的更广泛.当我在这个答案中说变量"时,是指JLS所说的" local 变量".

1 "local variable" vs. "variable" - The Java Language Specification uses "variable" in a more general way than most people do in common speech. When I say "variable" in this answer, I mean what the JLS calls a "local variable".

2 字段"-JLS在某些地方将字段称为变量"(在其他地方将字段称为字段"),因此在上面称为(1). :-)

2 "field" - The JLS calls fields "variables" in some places (and "fields" in other places), hence (1) above. :-)

这篇关于Java中的“作用域"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆