joewang704 joewang704 - 5 months ago 7
Java Question

Accessing variables from a parent class when using an instance of the child does not seem to be working

I'm trying to port a program in Java over to Scala, however, I'm running into an error.

I have a line that accesses

DistributedConfig.BOOTSTRAP_SERVERS_CONFIG
.

In Java, this works fine.

However, in Scala this gives the error

value BOOTSTRAP_SERVERS_CONFIG is not a member of org.apache.kafka.connect.runtime.distributed.DistributedConfig


Looking into the
DistributedConfig
class,
BOOTSTRAP_SERVERS_CONFIG
is actually not defined in the class, but defined in
WorkerConfig
which
DistributedConfig
extends. I've checked and the variable is public.
public static final String BOOTSTRAP_SERVERS_CONFIG
is the exact definition.

Answer

Java "fakes" inheritance of static members (when you write DistributedConfig.BOOTSTRAP_SERVERS_CONFIG, the Java compiler turns it into WorkerConfig.BOOTSTRAP_SERVERS_CONFIG), Scala doesn't because it wouldn't fit with Scala's approach to statics (using companion objects). So in Scala you can only use WorkerConfig.BOOTSTRAP_SERVERS_CONFIG.