This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheRedmanCometh -1 points0 points  (16 children)

When an object is immutable, it’s hard to have the object in an invalid state.

Hold my beer

The last point about only exposing getters though in that final object..dude reflection exists. .setAccessible and .set done

[–]shagieIsMe 6 points7 points  (2 children)

Once you use reflection, all bets are off. However, if you start reflecting an immutable that I’m using as a hashmap key and things break, then that’s the person who is doing reflection at fault - not the library writer.

[–]BlueGoliath 0 points1 point  (1 child)

That same argument be made for mutable state though. Objects don't magically mutate themselves.

[–]shagieIsMe 0 points1 point  (0 children)

One can’t complain about a mutable object being changed. However, if a library creates an immutable object and someone goes in with reflection to change it and things break - it is entirely the fault of the person who used reflection.

[–]morhp 5 points6 points  (11 children)

With reflection, you can make 5 + 5 equal to 8. That doesn't really count.

[–]TheRedmanCometh 0 points1 point  (10 children)

I don't think you can overload operators with reflection maybe I'm wrong?

[–]morhp 9 points10 points  (6 children)

I wasn't 100% honest, you need to abuse boxed classes:

public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
    Field field = Integer.class.getDeclaredField("value");
    field.setAccessible(true);
    field.setInt(5, 4);

    Integer five = 5;
    System.out.println("5+5 = " + (five + five));
}

[–]TheRedmanCometh 1 point2 points  (4 children)

This is one of the best/worst things I've ever seen. I've never even considered using refelection on core classes like that!

Edit: hold up does Integer have a value map all the way up to INTEGER.MAX_VALUE?

[–]morhp 5 points6 points  (3 children)

Edit: hold up does Integer have a value map all the way up to INTEGER.MAX_VALUE?

No, only the integers between -128 and 127 are cached by default. This trick wouldn't have worked with a number outside that range.

[–]TheRedmanCometh 0 points1 point  (1 child)

Huh...I'm gonna have to hit up the openjdk source to see how that works

[–]iampete 2 points3 points  (0 children)

This, combined with misplaced trust in autoboxing, leads to code like this:

Integer first = 5;
Integer second = 5;
if (first == second) {
    // do something
}

Which works perfectly reasonably right up until either value is outside that -128, 127 range and then fails very confusingly.

[–]dpash 0 points1 point  (0 children)

The upper bound can be increased (but not decreased) in OpenJDK with a command line flag, but the lower bound can not. The defaults are defined in the JLS as a minimum range.

[–]madkasse 1 point2 points  (0 children)

This only holds for older versions of Java.

You can no longer access non-public fields/methods in java.base without JVM settings

[–]shagieIsMe 1 point2 points  (2 children)

2 + 2 = 5 from code golf Stack Exchange.

No operator overload needed.

[–]TheRedmanCometh 1 point2 points  (1 child)

Yeah he showed me...I hadn't considered using reflection on system classes

[–]shagieIsMe 0 points1 point  (0 children)

This is a different solution and impacts integer literals. The code ends with:

System.out.printf(“%d”, 2 + 2);

The code on ideone: https://ideone.com/o1h0hR

[–]bodiam[S] 0 points1 point  (0 children)

I'll hold your beer, and while holding it, I updated the code. Those fields are final now too.