22 May, 2014

What will happen if you create a package java.lang and create a class String in it..?

Can we create and compile a class String, with package name java.lang ? Will it override the functionality of the existing class present in JVM ?
Surprizingly, we can create any class in any package, and if basic rules of compilation are followed, it will compile fine. Going by that, we can actually compile a class String in java.lang Package. But at run-time, JVM's java.lang package has a higher priority over user created java.lang package, hence original class will be referred.Try creating a String class with a method "printMyName() in java.lang package. Create a main class and call that method. The program will not give any compilation error (as at compile time the class u created is being referred) but at run time you will get java.lang.NoSuchMethodError (as the original class which is present in JVM is being referred and it does not contains this method)

The class which we created will be referred to at the time of compilation. So if we have written code considering the String class that we wrote, it will compile fine. But at runtime, as the JVM's classes have higher priority, original String class will be invoked and the code would behave buggy.