在 Java 中调用另一个方法中的变量

在本教程中,我们将学习如何在 Java 中调用另一个方法中的变量。这取决于变量的类型和它在类内的作用域。

Java 中在同一个类内的静态方法中调用一个静态变量

在同一个类中声明的静态变量可以在 main 方法和其他方法中被访问。在下面的例子中,在 main 方法的作用域内声明的变量 val 只能在该作用域内使用,而静态变量 y 则在其他静态方法内访问。

我们可以访问受范围限制的变量,将其传递给我们打算访问该变量的方法。

public class CallAVariable {
    static int y = 4; //declared inside class scope.
    public static void main(String[] args) {
        String val = "Hello"; //declared inside the scope of main method hence available in main only.
        System.out.println("In Main where static variable y is: "+y);
        callInNormal(val);
    }
    public static void callInNormal (String val){
        System.out.println("Value of static variable y in a static method is : "+y +" and String passed is: "+val);
    }
}

输出:

In Main where static variable y is: 4
Value of static variable y in a static method is : 4 and String passed is: Hello

Java 中从同一个类中的非静态方法调用静态变量

变量 y 是静态的,但是访问它的方法是非静态的。因此,我们需要创建一个类的实例来访问该方法和非静态变量 x

public class CallAVariable {
    int x = 2;
    static int y = 6;
    public static void main(String[] args) {
        //since the method is non static it needs to be called on the instance of class.
        //and so does the variable x.
        CallAVariable  i = new CallAVariable();
        System.out.println("In Main where static variable y is: "+y+ " and x is: "+i.x);
        i.callInNormal(i.x);
    }
    public void callInNormal (int x){
        CallAVariable i = new CallAVariable();
        System.out.println("Non static variable x is : " +x+" and static variable y is: "+y);
    }
}

输出:

In Main where static variable y is: 6 and x is: 2
Non static variable x is : 2 and static variable y is: 6

在 Java 中调用另一个构造函数

在 Java 中,对象初始化后调用构造函数的顺序称为构造函数链。当我们想通过使用一个实例来调用一个构造函数时,就会用到它。

在 Java 中从同一个类中调用一个构造函数

当我们想从同一个类的另一个构造函数中调用一个构造函数时,我们使用 this 关键字。使用 this 关键字的表达式必须是构造函数的第一行。在构造函数链中,顺序并不重要。它必须有至少一个不使用 this 关键字的构造函数。

在下面的例子中,我们有一个类 Test,它有 3 个构造函数。其中一个是没有任何参数的默认构造函数,而另外两个构造函数则是参数化的。在 main 方法中,我们用 new 关键字实例化类对象,没有传递任何参数。

它调用默认的构造函数 Test(),使用 this("Steve") 重定向调用参数化的构造函数 Test(String name)。构造函数 Test(String name) 进一步将调用重定向到另一个参数化的构造函数 Test(int age,String place),使用 this(30,"Texas")

Test(int age, String place) 里面的语句首先被执行。之后,执行 Test(String name) 构造函数的其余语句。最后,执行默认构造函数的剩余语句,对象初始化成功。

public class Test {
    Test() {
        this("Steve");
        System.out.println("Default Constructor");
    }
    Test(String name){
        this(30,"Texas");
        System.out.println("Name - "+name);
    }
    Test(int age, String place){
        System.out.println("Age- "+age+", place- "+place);
    }
    public static void main (String [] args){
            Test test = new Test();
    }
}

输出:

Age- 30, place- Texas
Name - Steve
Default Constructor

在 Java 中从基类中调用另一个构造函数

当有多个类有继承关系时,我们需要使用 super 关键字从子类调用父类的构造函数。与 this 关键字不同,JVM 会自动把 super 关键字。

基类是由派生类扩展的父类。我们在实例化类对象的主方法中,调用了同一类的默认构造函数,该方法使用 this("Adam") 进一步重定向调用子类的参数化构造函数。

super(name) 调用 Base 类的参数化构造函数 Base(String name)。使用 this() 调用 Base 类的默认构造函数。类的变量也在这里使用 this.name = name 进行初始化。

Base 类的缺省构造函数里面的代码就执行了。之后,参数化构造函数 Base(String name) 里面的语句执行。最后,执行剩余的 Derived 类的参数化构造函数里面的语句。

class Base {
    String name;
    Base(){
        System.out.println("Default Constructor Base Class");
    }
    Base(String name){
        this();
        this.name = name;
        System.out.println("Parameterized Constructor Base Class called:- "+name);
    }
}
class Derived extends Base {
    Derived() {
        this("Adam");
    }
    Derived(String name) {
        super(name);
        System.out.println("Parameterized Constructor Derived Class called: "+name);
    }
    public  static  void main (String [] args) {
        Derived derived = new Derived();
    }
}

输出:

Default Constructor Base Class
Parameterized Constructor Base Class called:- Adam
Parameterized Constructor Derived Class called: Adam