Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的重复引用变量_Java - Fatal编程技术网

java中的重复引用变量

java中的重复引用变量,java,Java,我有两个java类,一个是main方法,另一个是私有变量&获取和设置的方法。所以,我的问题是,我们可以在所有setter(3个setter)中使用相同的对象引用变量,在所有getter(3个)中使用不同的引用变量吗? 我用了它,得到了空值 但是,当我为3个getter使用3个不同的对象引用变量,并在中为getter使用相同的3个对象引用变量时,它工作正常。 有人能给我解释一下这个概念吗 一揽子计划1 公共类封装Jerry1{ public static void main(String[] ar

我有两个java类,一个是main方法,另一个是私有变量&获取和设置的方法。所以,我的问题是,我们可以在所有setter(3个setter)中使用相同的对象引用变量,在所有getter(3个)中使用不同的引用变量吗? 我用了它,得到了空值

但是,当我为3个getter使用3个不同的对象引用变量,并在中为getter使用相同的3个对象引用变量时,它工作正常。 有人能给我解释一下这个概念吗

一揽子计划1

公共类封装Jerry1{

public static void main(String[] args)
{
    System.out.println("Now, we are gonna test our Encapsulation");

    // I gave different variables for all setters and same respective variables for getters. This is working fine.
    Encapsulationtom1 obj1 = new Encapsulationtom1();
    Encapsulationtom1 obj2 = new Encapsulationtom1();
    Encapsulationtom1 obj3 = new Encapsulationtom1();

    obj1.setDesignation("Lead Designer");
    obj2.setEmployeeId(23452);
    obj3.setEmployeeName("Meliodas");

    System.out.println("The designation is "+ obj1.getDesignation());
    System.out.println("The Employee Id is "+ obj2.getEmployeeId());
    System.out.println("The Employee Name is "+ obj3.getEmployeeName());

    // But when i give same variable to all setters and a different variable to all getters, it gave me null value.   WHY?
}

}

获取空值的原因是您没有为第二种情况设置这些变量的值。例如,让您执行以下操作:

    Encapsulationtom1 obj1 = new Encapsulationtom1();
    Encapsulationtom1 obj2 = new Encapsulationtom1();
    Encapsulationtom1 obj3 = new Encapsulationtom1();

    obj1.setDesignation("Lead Designer");
    obj1.setEmployeeId(23452);
    obj1.setEmployeeName("Meliodas");

    System.out.println("The designation is "+ obj1.getDesignation());
    System.out.println("The designation is "+ obj2.getEmployeeId());
    System.out.println("The designation is "+ obj3.getEmployeeName());
这两条线:

    System.out.println("The designation is "+ obj2.getEmployeeId());
    System.out.println("The designation is "+ obj3.getEmployeeName());

将返回
Null
,因为您尚未给它们一个值。因此,
obj2
obj3
的变量尚未实例化,调用它们将返回
null

获取空值的原因是您没有为第二种情况设置这些变量的值。例如,让您执行以下操作:

    Encapsulationtom1 obj1 = new Encapsulationtom1();
    Encapsulationtom1 obj2 = new Encapsulationtom1();
    Encapsulationtom1 obj3 = new Encapsulationtom1();

    obj1.setDesignation("Lead Designer");
    obj1.setEmployeeId(23452);
    obj1.setEmployeeName("Meliodas");

    System.out.println("The designation is "+ obj1.getDesignation());
    System.out.println("The designation is "+ obj2.getEmployeeId());
    System.out.println("The designation is "+ obj3.getEmployeeName());
这两条线:

    System.out.println("The designation is "+ obj2.getEmployeeId());
    System.out.println("The designation is "+ obj3.getEmployeeName());

将返回
Null
,因为您尚未给它们一个值。因此,
obj2
obj3
的变量尚未实例化,调用它们将返回
null

让我们看一些代码。一个好的代码示例值千言万语。。。它的pasteof.code链接:DLET看到一些代码。一个好的代码示例胜过千言万语。。。它的pasteof.code链接:DOK,我想我得到了:D用于设置值的相同对象可以用于获取值:DYup!如果不在对象中设置值,将无法获取该值,它将返回
null
ok,非常感谢您的帮助。我只是暂时没有连接这些东西,但现在一切都很完美:太棒了!别忘了投票表决!好的,我想我明白了:D用于设置值的相同对象可以用于获取值:DYup!如果不在对象中设置值,将无法获取该值,它将返回
null
ok,非常感谢您的帮助。我只是暂时没有连接这些东西,但现在一切都很完美:太棒了!别忘了投票表决!