Inputstream is = getclass (). getclassloader (). getresourceasstream ("helloworld. properties"); what does getclass () and getclassloader () mean.
Getclass (): obtains the class object to which the current object belongs.
Getclassloader (): gets the class loader of this class object.
The class loader is responsible for reading the attention stream from the Java character file into the memory and constructing the class object. You can use it to get the input stream of a file.
Getclass:
Public final class getclass ()
Returns the runtime class of an object. That class object is the object that is locked by static synchronized methods of the represented class.
Returns:
The object of type class that represents the runtime class of the object.
Getclassloader
Public classloader getclassloader ()
Returns the Class Loader for the class. Some implementations may use null to represent the bootstrap Class Loader. This method will return NULL in such implementations if this class was loaded by the bootstrap class loader.
If a security manager is present, and the caller's class loader is not null and the caller's class loader is not the same as or an ancestor of the Class Loader for the class whose class loader is requested, then this method callthe security manager's checkpermission method with a runtimepermission ("getclassloader") permission to ensure it's OK to access the Class Loader for the class.
If this object represents a primitive type or void, null is returned.
Returns:
The class loader that loaded the class or interface represented by this object.
Throws:
Securityexception-if a security manager exists and its checkpermission method denies access to the class loader for the class.
See also:
Classloader, securitymanager. checkpermission (Java. Security. Permission), runtimepermission
A small trap of class. getclassloader :)
Yesterday, my code was always in integer. class. getclassloader (). getresource ("***********"); this sentence throws a null pointer exception, locates it as getclassloader () and returns NULL. Check the JDK documentation, there is also a trap here:
The description of getclassloader () in JDK:
/**
* Returns the Class Loader for the class. Some implementations may use
* Null to represent the bootstrap Class Loader. This method will return
* Null in such implementations if this class was loaded by the bootstrap
* Class loader.
*
* <P> if a security manager is present, and the caller's class loader is
* Not null and the caller's class loader is not the same as or an ancestor
* The Class Loader for the class whose class loader is requested, then
* This method callthe security manager's <code> checkpermission </code>
* Method with a <code> runtimepermission ("getclassloader") </code>
* Permission to ensure it's OK to access the Class Loader for the class.
*
* <P> if this object
* Represents a primitive type or void, null is returned.
.....
The above English can be understood as follows:
The process of loading classes is very simple: Find the class location, and load the byte code of the found Java class into the memory to generate the corresponding class object. Java classloaders are used to implement such a process. JVM does not have more than one classloader. In fact, if you want to, you can make the JVM have countless classloaders, of course, in addition to testing JVM, I cannot think of other purposes. You should have discovered this problem. The class loader itself is also a class and it also needs to be loaded into the memory. Who will load these class loaders, is there always a root? Yes, there is such a root. It is the bootstrap classloader that never comes to an end. why is it so fascinating? Because you cannot catch a little bit of its tail in Java code, even though you can understand its existence all the time, because all the class libraries required by the Java Runtime Environment are loaded by it, and it is a C ++ program, which can be run independently. It can be said that it is the starting point of JVM running, great. After Bootstrap completes its task, it will generate an appclassloader (in fact, the system will also use the extended Class Loader extclassloader, which is used to load classes in the Java Runtime Environment extension package ), this class loader is often used. You can call classloader. getsystemclassloader () is obtained. Assume that the program does not use the classloader-related operation settings or customize the new classloader, all Java classes we write will be loaded by it, it is worthy of respect. The area of the appclassloader search class is the familiar classpath, which must be crossed by beginners. Is there any flashing sensation? Let's name it the class path Class Loader according to its class search range. In the previous assumption, when a new class appears in Java, appclassloader first passes the class to its parent class loader, that is, extion classloader, and asks whether it can load the class, if yes, appclassloader won't do it. Similarly, when the extion classloader is loading, it will first ask its parent class loader. We can see that the class loader is actually a tree structure. Each class loader has its own father. When the class loader loads the class, always let your parent loader load (Dear Elders). If the parent loader cannot load the class, you will load it. If it cannot load it, I'm sorry, it will yell: exception, class not found. It is necessary to mention that when a class fails to be loaded using the class path loader, The noclassdeffoundexception exception is thrown. If you use the custom Class Loader loadclass method or the findsystemclass method of classloader to load the class, if you do not deliberately change it, classnotfoundexception is thrown.
Here, JDK tells us: If a class is loaded through Bootstrap, then we get classloader through this class, some JDK implementations will return a null, for example, I use new object (). getclass (). if getclassloader () is used, a null value is returned. In this way, the above Code will encounter an nullpointer exception. therefore, we 'd better use our own classes to obtain classloader ("this. getclass (). getclassloader.