Ajit Bisht
Ajit Bisht

Reputation: 39

Class Loading in JAVA

The Sequence of searching and loading of classes by Class-Loader is:

  1. Bootstrap Class Loader
  2. Extension Class Loader
  3. Application Class Loader

Now if I create a user defined String class with constructor.

class String {
    public String(){
        System.out.println("This is user defined String Class");
    }
}

and then executes below code:

public class Checking {
    public static void main(java.lang.String[] args){
        String s= new String();
    }
}

Output of above code is "This is user defined String Class"

Which means user defined class is loaded, which is loaded by Application Class-Loader

So, my question is If bootstrap is loaded first , why am I getting the that output? I hope my question is clear.

Upvotes: 0

Views: 117

Answers (1)

Johannes H.
Johannes H.

Reputation: 6167

add the line

package java.lang

on top of your code, and check again. You will notice that you get your expected result now.

The reason for this is, that the class name is always only used as fully qualified name, which consists of the package name and the class name. So in your case, String is a different class than java.lang.String and will therefore not be found in rt.jar

Upvotes: 1

Related Questions