0

While practicing java code basics, I tried to name a class as _. It compiled successfully but when I ran it, an exception occurred:

import static java.lang.System.*;
public class _ {
public static void main(String[] __A_V_) {
    String $="";
    for (int x=0; ++x < __A_V_.length; $ += __A_V_[x]){
        out.println($);
    }
}
Exception in thread "main" java.lang.NoClassDefFoundError: _ (wrong name: RnD/_) 

Can someone clarify, why it compiled successfully but throws an exception at run time.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • 1
    It should not throw any error – Loki Sep 01 '16 at 04:48
  • 1
    I believe your package name is RnD or it's in a directory called RnD. This is a classpath error. However, _ is going to be a reserved name in a future version Java (probably Java 9) : https://stackoverflow.com/questions/23523946/underscore-is-a-reserved-keyword Here's a link to the bug showing that it has been addressed: https://bugs.openjdk.java.net/browse/JDK-8061549 – mttdbrd Sep 01 '16 at 04:51
  • Besides the syntactical problems - read java coding style guides. The only two places for `_` in Java are: CONSTANT_NAMES and number literals, like `float pi = 3.14_15F` - you do not use `_` for any other names. End of discussion. – GhostCat Sep 01 '16 at 04:56
  • 1
    @GhostCat: I agree that we must follow java coding style guides. Here my question why it compiled successfully but when I ran it, exception occurred. – Binay Sharma Sep 01 '16 at 05:05
  • @LokeshAgrawal: It throws an error, let me share the code snippet with you that may be helpful for you understand the problem. – Binay Sharma Sep 01 '16 at 05:07
  • Muha. Another no go: $ is a super bad name for a variable. – GhostCat Sep 01 '16 at 05:30
  • The problem has nothing to do with the name of your identifiers but all with the package that your class is in and from which directory you invoke your class. It's all in the description of the Error: `(wrong name: RnD/_) `. Look at the duplicate I suggested, that will answer your problem. – Erwin Bolwidt Sep 01 '16 at 05:41
  • @ErwinBolwidt Thanks Erwin for sharing it. I'll go through it......Many thanks again...:) – Binay Sharma Sep 01 '16 at 06:07

0 Answers0