Я написал код -Неожиданный выход в Java
// Node Class
class aNode {
// Node Contents
int NodeInt;
char NodeChar;
// constructor
aNode() {
}
aNode(int x, char y) {
NodeInt = x;
NodeChar = y;
}
}
class MainClass {
static aNode node = new aNode();
public static void main(String[] args) {
node = null;
function(node);
if (node == null) {
System.out.println("Node is null");
}
}
static void function(aNode x) {
if (x == null) {
System.out.println("Node is null");
}
x = new aNode(5, 'c');
System.out.println(x.NodeInt);
System.out.println(x.NodeChar);
}
}
Я ожидал, что выход будет -
Node is null
5
c
но когда программа возвращается к основной, значение узла устанавливается в нуль еще раз. Поэтому я получаю выход -
Node is null
5
c
Node is null
Пожалуйста, помогите мне изменить код, чтобы получить желаемый результат. Любая помощь будет оценена!
Взгляните на [это-Java-передача по ссылке] (HTTP: //stackoverflow.com/questions/40480/is-java-pass-by-reference?rq=1) – Pshemo