#AccessModifiers #threads #randomClass

Will the above code snippet compile or not?

Try to solve the challenge by yourself.

Let's understand the concept first —

There is a final class which means it can not be inherited. There is a thread also which consumes a Runnable having a run() method and it uses the constant from the Constants class, so now questions are —

  1. Can we use a constant from the final class in another class?
  2. Can Anonymous implementation of Runnable use the constant from another constant class?

Of course, a constant can be accessed from another class but the problem here is that you can only use static variables in static or top-level classes, and we’ve got a static variable in a class that isn’t top-level.

I hope you liked the challenge, if you do please subscribe to the publication for more such challenges.




Please try to solve the challenge by yourself.

This will challenge your String pool concept. If the content of the String is the same then it will refer to the same instance memory unless you have a new instance, but in the case of StringBuilder and StringBuffer, it does not refer to the same instance memory because when you call toString() method of StringBuilder and StringBuffer class, it creates the copy of the content with a new instance.

The implementation of StringBuffer -> toString() method —

The implementation of StringBuilder -> toString() method —