0

I was recently asked this question in an interview.

when a class with inner class is compiled there are two class files that are created. eg

class A{ class B{}}

So in this case 2 class files will be generated. A.class and A$B.class.

Why '$' symbol is used and not any other symbol? Does this question have a technical answer or is it subjective?

wib
  • 425
  • 3
  • 17
  • 1
    It is more or less arbitrary. – biziclop May 25 '17 at 11:35
  • 1
    Because why not? It could be one of ((*&^%$) or anything else, but $ was chosen. – Antoniossss May 25 '17 at 13:29
  • @Antoniossss, It cannot be just any symbol.The JLS specifies what characters are legal in an identifier, and most of the ones you mentioned are not. The JLS, which I commend to your attention, says that `$` is used for automatic, under-the-hood names, which is why it's used in generated class names. It's best to Read the Fine Manual. – Lew Bloch May 25 '17 at 13:40
  • 1
    @LewBloch But it is all about that it was *chosen*. World would not explode if someone would choose % for that - despite file system restrictions etc etc. – Antoniossss May 25 '17 at 13:46
  • `%` is not legal for an identifier in Java. See the JLS. It has nothing to do with file systems, "etc etc [sic]". Please read the JLS. – Lew Bloch May 25 '17 at 15:53

0 Answers0