Set custom class object's value with '=' operator in Java

前端 未结 6 2023
滥情空心
滥情空心 2021-01-19 01:31

I have a custom object that has a single value of type int that I wanting to do processing on to keep this value in a set range. My question is this: Given the following cla

6条回答
  •  [愿得一人]
    2021-01-19 02:16

    If you mean:

    foo x = new foo();
    x = 10; // This is meant to set x.bar
    

    then no, you can't do that in Java. Good thing too, if you ask me... it would be horrible in terms of readability.

    You also can't change it to allow:

    foo x = 10;
    

    as equivalent to:

    foo x = new foo();
    x.bar = 10; // Or x.setBar(10);
    

提交回复
热议问题