I was messing around with String and formatting earlier today and have found an API oddity.
Usually when I am formatting a string I use
String.format("mystr %s", 1);
or something similar.
Now this static method is also accessible to instantiated objects of this class. The code below shows the result of using String.format statically and using format method directly from an object.
@Test
public void test() {
assertEquals("abc 123", String.format("abc %s", 123));
assertEquals("abc 123", "abc".format("abc %s", 123));
assertEquals("def 123", "abc".format("def %s", 123));
}
The last assertEquals() bascially overwrites the first “abc” string which I do expect looking at the signature of format.
Am I missing some funky usage of using the static method from the instantiated object. Or is it just visible because the static String.format() is also accessbile to the instantiated objects of String? In other words an API oddity/bug.
All static methods can also be called on instance of their class.
Using this feature is generally not recommended (many tools track it) but this isn’t really a bug.