5

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 class, can I set it's value with myObject = 0;

public class foo{
    private int bar;
    public foo(){
    }
}

Instead of creating a method public void setBar()

Xandor
  • 440
  • 1
  • 9
  • 22

6 Answers6

5

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think it would be nice for working with matrices... but obviously not for this example. – sdasdadas Aug 22 '13 at 17:11
  • Access modifier of bar isn't public, so that has to be changed too if you want to set x.bar – Shiva Kumar Aug 22 '13 at 17:14
  • @ShivaKumar: I was assuming that would be the case. Although the code could be called from the same class, of course :) – Jon Skeet Aug 22 '13 at 17:14
  • 1
    Can't agree more. In my opinion the readability weighs more than that it is not possible to overload operators in Java. I wouldn't recommend doing it if it were possible. – Viktor Seifert Aug 22 '13 at 17:17
  • As far as the readability, I am essentially trying to create a custom data type that works like an int with a range constraint. In-use it will read much nicer that get/set methods(my whole reasoning in wanting this). I need to run some code when the value is set to make sure it is in range, otherwise throw an exception(this coding I have already done). So yes, it must be private unless (as asked below in a different comment) I am able to listen for a public value to be changed so a private method may be automatically called to process. – Xandor Aug 22 '13 at 18:11
4

No you can't do that. Java does not support operator overloading. Although + operator is overloaded for performing String concatenation, but that's the only exception. Another example that uses the = operator the way you would want is in case of wrapper classes, where you can directly assign a primitive type values to it's corresponding wrapper type, which causes the primitive value to be auto-boxed to wrapper type.

Integer val = 10;  // 10 is auto-boxed to Integer reference

But it's only limited for that purpose. You can't do that for your own user-defined type.

Creating a method is your only option.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

Foo myObject = new Foo();

Here, myObject holds the reference. You can't assign primitive value such as 0 to object references.

Instead, you should do myObject.setBar(10);

Vikas V
  • 3,176
  • 2
  • 37
  • 60
0

No, it goes against encapsulation logic, and Java Itself.

Abstract
  • 664
  • 1
  • 5
  • 15
0

In Java, the convention is to provide setters and getters to change an object's inner attributes. For your case:

foo instance = new foo();
instance.setBar(10); //Sets bar to 10
instance.getBar(); //returns bar's current value (right now 10)

The setter receives the new value and sets it:

public void setBar(int newBar) {
    bar = newBar;
}

And the getter gives access to the field's current value:

public int getBar() {
    return bar;
}

You cannot, however, overload the = operator to do as setBar does, at least in Java. If you're thinking about, for example the Integer or Float wrapper classes, there's another force at work there, related to Java's implementation itself and that later derives in the concepts of boxing and unboxing.

Fritz
  • 9,987
  • 4
  • 30
  • 49
0

Abother possibility, you could make this field public. It would just need to do the validations you need in the business method (no during the set).

lcestari
  • 178
  • 5
  • This is something I had considered, but then my question is, inside the constructor can I put some sort of listener on the then public field so that I may do some processing as soon as its changed without having to manually call some sort of .process() method? – Xandor Aug 22 '13 at 17:50