在Java中为返回变量赋值

Dim*_*ath 2 java reference

这可能是一个基本问题,但我不确定使用哪些关键字进行搜索.

是否可以在Java中为返回变量赋值,类似于:

static int a[] = new int[2];

static int f(int i) {
    return a[i];
}

static void main() {
    f(1) = 0; // <-- this
}
Run Code Online (Sandbox Code Playgroud)

在C/C++中,我可以返回一个指针并稍后为其赋值.由于Java使用引用,我希望上面的代码可以工作.我在这里错过了一些重要的Java概念吗?

小智 5

即使它是引用类型,方法invokation 也不是变量.

但是,这样的事情就可以了:

static MyClass func() {
   return new MyClass();
}

public static void main(String[] args) {
   func().setAttr(null); // change attributes
}
Run Code Online (Sandbox Code Playgroud)