Search code examples
javaandroidsyntax

null keyword on class declaration


Out of curiosity, I was looking through the files of a simple Android application developed for controlling a machine (I didn't develop it, which isn't relevant to the question, I suppose). I'm trying to get the app running on my laptop to see how it works. In the code, I found many appearances of the following:

class null implements AdapterViewICS.OnItemClickListener 
{

  //... some other stuff

}

I have no experience with Java code at all, but when using Android Studio to view these files the null keyword is marked as an error: java: <identifier> expected, which seems reasonable although I don't understand how this happened (I didn't change the code and the app works on my phone). My hypothesis is that most of these classes aren't used in the actual application and that's why these errors never caused trouble (if they are even errors at all), but they are now preventing me from running on my computer and I'd like to fix that.

Could anyone with Java experience tell me if this is correct, or explain why this could have happened (maybe some quirk in the decompiling?)

I've tried looking up class definition syntax for Java but I haven't found anything about null being used as an accesor, return type, or anything as such.

Also when changing null to Null the error disappears (as expected) and the class is marked with no usages. This confuses me because I'm unsure if the class wasn't ever used in the first place or doesn't appear now since I changed its identifier.


Solution

  • Also when changing null to Null the error disappears (as expected) and the class is marked with no usages.

    Of course; java is, like most programming languages, case sensitive. a class named null and a class named Null as different as a class named Guns and a class named Grandmas.

    Insofar that null was somehow used, changing that to Null would, obviously, mean nobody uses it - they'd be using null, not Null.

    class null

    That'd be a decompiler error... probably.

    Java-the-language dictates that classes cannot have a keyword as a name, and null is a keyword. Hence, javac refuses to compile this.

    However, java-the-virtual-machine doesn't actually care. Some JVM-targeted languages allow you to 'escape' a keyword, such as scala or kotlin with backticks where you really could write:

    // Except in scala syntax of course
    class `null` { ... }
    

    But java-the-language isn't one of those. You cannot make javac produce the class file representing a class named null no matter how hard you try, but with other compilers, or some byte code hacking (modifying a .class file), you can, and java.exe would run it just fine.

    it is likely the decompiler messed up. Decompilers are crappy and will likely always be, because professionals are effectively never in a situation where they need to decompile (just get the source), and no amount of awesomeness in a decompiler is going to restore comments for you, and comments are very useful.

    However, an obfuscator might have gone for the hack of naming one class in every package null, because in class files only you can actually pull that off. I'd expect other keywords too if that is the case - classes named for, while, class, interface, and so on.