Java 如果我在一个方法中给一个变量赋值,它是否会超出该方法的作用域?

Java 如果我在一个方法中给一个变量赋值,它是否会超出该方法的作用域?,java,Java,抱歉,如果这是重复,我已尝试寻找我的问题的答案,但无法找到我要的答案 我对Java非常陌生,从昨天开始,我试图理解为什么如果我在类中声明一个变量,然后在一个void方法中为它赋值,那么与该变量相关联的值就会超出该方法的范围。我对方法的理解非常有限,因为它们有自己的作用域,我认为访问方法中所做的任何变量更改的唯一方法是在方法末尾返回它们。不是这样吗?这个问题的答案有点复杂 首先开始区分方法和函数 方法通过调用类的构造函数对实例化的对象进行操作,例如MyClass obj=new MyClass;。

抱歉,如果这是重复,我已尝试寻找我的问题的答案,但无法找到我要的答案


我对Java非常陌生,从昨天开始,我试图理解为什么如果我在类中声明一个变量,然后在一个void方法中为它赋值,那么与该变量相关联的值就会超出该方法的范围。我对方法的理解非常有限,因为它们有自己的作用域,我认为访问方法中所做的任何变量更改的唯一方法是在方法末尾返回它们。不是这样吗?

这个问题的答案有点复杂

首先开始区分方法和函数

方法通过调用类的构造函数对实例化的对象进行操作,例如MyClass obj=new MyClass;。从这一点来看,对象有一个由其成员变量表示的状态—所有变量在类中都不是静态的。 作为对象一部分的方法可能会更改此状态,除非相应的变量声明为final

要做到这一点,方法继承类的作用域,但变量绑定到调用方法的对象实例

例如:

public class MyClass {
    private int a = 0;
    public void aplusplus() {a++;}
}

MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
obj1.aplusplus();  // the a of obj1 is now 1
obj2.aplusplus();  // the a of obj2 is now 1
obj1.aplusplus();  // the a of obj1 is now 2
public class MyClass {
    private int a = 0;
    public void aplusplus() {a++;}
    public void setA(int a) {this.a = a;}
}
但要当心!还有一个概念叫做隐藏。 如果在方法体中声明局部变量,它将隐藏类成员变量。按名称访问变量会导致访问局部变量,而不是成员变量。当为私有成员实现setter方法时,这一点特别有趣,因为方法的参数(局部变量)通常与成员变量完全相同。要仍然访问成员变量,可以使用this.variablename。例如:

public class MyClass {
    private int a = 0;
    public void aplusplus() {a++;}
}

MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
obj1.aplusplus();  // the a of obj1 is now 1
obj2.aplusplus();  // the a of obj2 is now 1
obj1.aplusplus();  // the a of obj1 is now 2
public class MyClass {
    private int a = 0;
    public void aplusplus() {a++;}
    public void setA(int a) {this.a = a;}
}

类中的每个方法都将继承任何属性变量或直接属于该类的任何方法。说

public class Bicycle {

    public int gear;
    public int speed;

    // the Bicycle class has
    // two  methods

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void speedUp(int increment) {
        speed += increment;
    }
}
让我们来看看setGear方法

public void setGear(int newValue) {
    gear = newValue;
}
如您所见,我可以访问“gear”属性,因为它属于Bicycle类。参数'int newValue'专门属于此方法,这意味着我只能在此方法内访问此变量,如下所示:

public void setGear(int newValue) {
    newValue = gear;
    //this wouldn't make much sense logic wise, but it is
    //valid code since I can use newValue only inside the setGear method

    speedUp(10);
    //the method 'speedUp' belongs to the bicycle class, so I can use
    //it inside another method belonging to the same class
}
或者,我可以使用this关键字表示我引用的是类属性,如下所示:

public void setGear(int gear) {
    this.gear = gear;
    //I am telling the method that the class attribute 'gear' will 
    //receive my parameter value 'gear'
    //this is useful when my parameter variable name has the same 
    //name as my class attribute
}

编辑:忘记给官方的oracle文档评分了,你在问题中说你在类中声明了一个变量。这意味着在方法调用期间,变量设置为的任何值都将在方法调用期间保持不变。变量是该对象的一部分,因此称为实例变量


如果在方法定义内声明变量,则它们在方法的作用域内。他们是这个方法的一部分。这些变量被称为方法的局部变量。只能在方法内部使用这些变量。此对象不知道它们存在。

方法执行后,方法中的局部变量不再存在,但类/实例变量不存在。请使用参考书和bit练习help@Mr.Arjun我以为这就是StackExchange的目的?在一个问题上得到有帮助和友好的答案。不被提及更多的技术性阅读?@vcromy这不是对任何人粗鲁或友善。但这只是在一些琐碎的事情上节省某人的生产时间。请查看StackExchange的帮助部分。这里的所有答案我们都很好,但这一条的深度和简单性让我受益匪浅。谢谢