If I replace a class with a private constructor by...
# library-development
c
If I replace a class with a private constructor by an interface… AFAIK this is a source-compatible change, but is it a binary-compatible change?
e
with only a private constructor? that doesn't sound like a useful class
c
It has members, but I would keep their signature identical in the interface. My point is, external users couldn't instantiate it anyway, so is the class/interface somewhat relevant at the binary level, which would make this a breaking binary change even though it's source-safe?
e
on JVM class member methods are called with the
invokevirtual
bytecode instruction while interface methods are called with
invokeinterface
. there's some subtle difference between the two. I can't think of anything that will outright break due to the difference, I don't think the verifier cares… but I'm not sure
c
Hm, so at the very best it's still quite shady. Thanks for the info
m
Got curious and tried it out, I got
Copy code
Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface a.A, but class was expected
	at main.Main.main(Main.java:7)
e
ah I guess the verifier does care then
m
Looks like it
Can’t find any reference of it in the spec though
Source code is here if you fancy cpp 🙂
e
ah, I didn't check the spec. it does mention this, https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.4.3.3
When resolving a method reference: If C is an interface, method resolution throws an
IncompatibleClassChangeError
.
🎯 1
m
Ah good find 🙌
It’s all consistent 👌
c
Thanks a lot, that's very useful to know! Not the answer I would have preferred, but oh well 😅