Java中变量的作用域

变量的作用域是程序中可访问变量的部分。与Java中的C/C++一样,所有标识符都在词汇(或静态)范围内,即变量的范围可以在编译时确定,并且与函数调用堆栈无关。 Java程序是以类的形式组织的。每门课都是某个包的一部分。Java范围规则可以分为以下几类。

null

成员变量(类级别范围)

这些变量必须在类内声明(在任何函数外)。它们可以在课堂上的任何地方直接访问。让我们看一个例子:

public class Test{    // All variables defined directly inside a class     // are member variables    int a;    private String b;    void method1() {....}    int method2() {....}    char c;}
  • 我们可以在类中的任何地方声明类变量,但在方法之外。
  • 访问指定的成员变量不会影响它们在类中的作用域。
  • 可以使用以下规则在类外部访问成员变量
Modifier      Package  Subclass  Worldpublic          Yes      Yes     Yesprotected       Yes      Yes     NoDefault (nomodifier)       Yes       No     Noprivate         No        No     No

局部变量(方法级范围)

方法内声明的变量具有方法级作用域,不能在方法外访问。

public class Test{    void method1()     {       // Local variable (Method level scope)       int x;    }}

注: 方法执行结束后,局部变量不存在。

这里是方法作用域的另一个例子,除了这次变量作为参数传递给方法:

class Test{    private int x;    public void setX(int x)    {        this.x = x;    }}

上面的代码使用 这个关键词 区分局部变量和类变量。

作为练习,预测以下Java程序的输出。

JAVA

public class Test
{
static int x = 11 ;
private int y = 33 ;
public void method1( int x)
{
Test t = new Test();
this .x = 22 ;
y = 44 ;
System.out.println( "Test.x: " + Test.x);
System.out.println( "t.x: " + t.x);
System.out.println( "t.y: " + t.y);
System.out.println( "y: " + y);
}
public static void main(String args[])
{
Test t = new Test();
t.method1( 5 );
}
}


输出:

Test.x: 22t.x: 22t.y: 33y: 44

循环变量(块范围) 方法中括号“{”和“}”内声明的变量的作用域仅在括号内。

JAVA

public class Test
{
public static void main(String args[])
{
{
// The variable x has scope within
// brackets
int x = 10 ;
System.out.println(x);
}
// Uncommenting below line would produce
// error since variable x is out of scope.
// System.out.println(x);
}
}


输出:

10

作为另一个例子,考虑下面的带有for循环的程序。

JAVA

class Test
{
public static void main(String args[])
{
for ( int x = 0 ; x < 4 ; x++)
{
System.out.println(x);
}
// Will produce error
System.out.println(x);
}
}


输出:

11: error: cannot find symbol        System.out.println(x);      

正确的方法是,

JAVA

// Above program after correcting the error
class Test
{
public static void main(String args[])
{
int x;
for (x = 0 ; x < 4 ; x++)
{
System.out.println(x);
}
System.out.println(x);
}
}


输出:

01234

让我们来看一个复杂的循环范围示例。预测以下程序的输出。如果你是普通的C/C++程序员,你可能会感到惊讶。

JAVA

class Test
{
public static void main(String args[])
{
int a = 5 ;
for ( int a = 0 ; a < 5 ; a++)
{
System.out.println(a);
}
}
}


输出:

6: error: variable a is already defined in method go(int)       for (int a = 0; a < 5; a++)                       ^1 error

注意:在C++中,它运行。但在java中这是一个错误,因为在java中,内循环和外循环变量的名称必须不同。 类似的程序在C++中工作。看见 .

作为练习,预测以下Java程序的输出。

JAVA

class Test
{
public static void main(String args[])
{
{
int x = 5 ;
{
int x = 10 ;
System.out.println(x);
}
}
}
}


问:根据以上知识,判断下面的代码是否会运行。

JAVA

class Test {
public static void main(String args[])
{
for ( int i = 1 ; i <= 10 ; i++) {
System.out.println(i);
}
int i = 20 ;
System.out.println(i);
}
}


输出:

1234567891020

是的,它会跑的! 仔细查看程序,内部循环将在声明外部循环变量之前终止。因此,先销毁内部循环变量,然后创建同名的新变量。

关于Java中变量作用域的一些要点:

  • 通常,一组花括号{}定义了一个作用域。
  • 在Java中,我们通常可以访问一个变量,只要它是在与我们正在编写的代码相同的一组括号内定义的,或者是在定义变量的花括号内的任何花括号内定义的。
  • 在任何方法之外的类中定义的任何变量都可以被所有成员方法使用。
  • 当一个方法与一个成员具有相同的局部变量时,“this”关键字可用于引用当前类变量。
  • 对于在循环结束后读取的变量,必须在循环体之前声明它。

本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享