⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javalang.doc13.html

📁 java语言规范
💻 HTML
字号:
<html>
<head>
<title>The Java Language Specification The Package java.lang </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="javalang.doc12.html">Prev</a> | <a href="javalang.doc14.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="14462"></a>
<center><h1>20.14  The Class  <code>java.lang.ClassLoader</code></h1></center>
<a name="28743"></a>
A class loader is an object that is responsible for loading classes. Given the name 
of a class, it should attempt to locate or generate data that constitutes a definition 
for the class. A typical strategy is to transform the name into a file name and then 
read a "class file" of that name from a file system.
<p><a name="28756"></a>
Every <code>Class</code> object contains a reference to the <code>ClassLoader</code> that defined it <a href="javalang.doc2.html#13804">(&#167;20.3.7)</a>. Whenever executable Java code needs to use a class that has not yet been loaded, the <code>loadClass</code> method is invoked for the class loader of the class containing the code in question.<p>
<a name="36892"></a>
Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by the <code>getClassLoader</code> method of class <code>Class</code> <a href="javalang.doc2.html#13804">(&#167;20.3.7)</a>, is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.<p>
<a name="28770"></a>
Class loaders may typically be used by security managers <a href="javalang.doc16.html#46274">(&#167;20.17)</a> to indicate security domains: two classes may considered to be "friendly" or "related" to each other only if they were defined by the same class loader.<p>
<pre><a name="14059"></a>public abstract class <code><b>ClassLoader</b></code> {
<a name="3359"></a>	protected <code><b>ClassLoader</b></code>() throws SecurityException;
<a name="28812"></a>	protected abstract Class <code><b>loadClass</b></code>(String name,
<a name="25729"></a>		 &#32; &#32; &#32;boolean resolve)<br>
		throws ClassNotFoundException;
<a name="3077"></a>	protected final Class <code><b>defineClass</b></code>(byte data[],
<a name="28873"></a>		 &#32; &#32; &#32;int offset, int length)<br>
		throws NullPointerException, IndexOutOfBoundsException,
<a name="28876"></a>			ClassFormatError;
<a name="3078"></a>	protected final void <code><b>resolveClass</b></code>(Class c)<br>
		throws NullPointerException;
<a name="3079"></a>	protected final Class <code><b>findSystemClass</b></code>(String name)<br>
		throws ClassNotFoundException;
<a name="3083"></a>}
</pre><a name="14060"></a>
<p><font size=+1><strong>20.14.1   </strong> <code>protected <code><b>ClassLoader</b></code>() throws SecurityException</code></font>
<p>
<a name="28792"></a>
This constructor is invoked for every newly created class loader. Because the class 
<code>ClassLoader</code> is <code>abstract</code>, it is not possible to create a new instance of the class 
<code>ClassLoader</code> itself; however, every constructor for a subclass of <code>ClassLoader</code> 
necessarily invokes this constructor, explicitly or implicitly, directly or indirectly.
<p><a name="28823"></a>
All this constructor does is to enforce a security check: if there is a security manager, its <code>checkCreateClassLoader</code> method <a href="javalang.doc16.html#14108">(&#167;20.17.10)</a> is called.<p>
<a name="14061"></a>
<p><font size=+1><strong>20.14.2   </strong> <code>protected abstract Class <code><b>loadClass</b></code>(String name,<br> &#32; &#32; &#32;boolean link)<br>throws ClassNotFoundException</code></font>
<p>
<a name="28824"></a>
Every subclass of <code>ClassLoader</code> that is not itself abstract must provide an implementation
of the method <code>loadClass</code>.
<p><a name="28825"></a>
The general contract of <code>loadClass</code> is that, given the <code>name</code> of a class, it either returns the <code>Class</code> object for the class or throws a <code>ClassNotFoundException</code>.<p>
<a name="28845"></a>
If a <code>Class</code> object is to be returned and <code>link</code> is <code>true</code>, then the <code>Class</code> object should be linked (<a href="12.doc.html#44487">&#167;12.3</a>, <a href="javalang.doc13.html#28858">&#167;20.14.4</a>) before it is returned.<p>
<a name="35725"></a>
In most cases, it is wise for a subclass of <code>ClassLoader</code> <a href="javalang.doc13.html#14462">(&#167;20.14)</a> to implement the <code>loadClass</code> method as a <code>synchronized</code> method.<p>
<a name="35720"></a>
<p>
<a name="52452"></a>
<p><font size=+1><strong>20.14.3   </strong> <code>protected final Class <code><b>defineClass</b></code>(byte data[],<br> &#32; &#32; &#32;int offset, int length)<br>throws NullPointerException,  &#32; &#32; &#32;IndexOutOfBoundsException, ClassFormatError</code></font>
<p>
<a name="52453"></a>
This method may be used by a class loader to define a new class.
<p><a name="28867"></a>
The bytes in the array <code>data</code> in positions <code>offset</code> through <code>offset+length-1</code> should have the format of a valid class file as defined by the <i>Java Virtual Machine Specification</i>.<p>
<a name="28878"></a>
If <code>data</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="28879"></a>
An <code>IndexOutOfBoundsException</code> is thrown if any of the following are true:<p>
<ul><a name="28880"></a>
<li><code>offset</code> is negative
<a name="28881"></a>
<li><code>length</code> is negative
<a name="28882"></a>
<li><code>offset+length</code> is greater than <code>data.length</code>
</ul><a name="28883"></a>
If the indicated bytes of <code>data</code> do not constitute a valid class definition, then a <code>ClassFormatError</code> is thrown. Otherwise, this method creates and returns a <code>Class</code> object as described by the data bytes<p>
<a name="28858"></a>
<p><font size=+1><strong>20.14.4   </strong> <code>protected final void <code><b>resolveClass</b></code>(Class c)<br>throws NullPointerException</code></font>
<p>
<a name="28889"></a>
This (misleadingly named) method may be used by a class loader to link (<a href="12.doc.html#44487">&#167;12.3</a>, 
<a href="javalang.doc13.html#28858">&#167;20.14.4</a>) a class.
<p><a name="28905"></a>
If <code>c</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="28903"></a>
If the <code>Class</code> object <code>c</code> has already been linked, then this method simply returns.<p>
<a name="28912"></a>
Otherwise, the class is linked as described in <a href="12.doc.html#44487">&#167;12.3</a>.<p>
<a name="28911"></a>
<p>
<a name="14065"></a>
<p><font size=+1><strong>20.14.5   </strong> <code>protected final Class <code><b>findSystemClass</b></code>(String name)<br>throws ClassNotFoundException</code></font>
<p>
<a name="28925"></a>
This method may be used by a class loader to locate a class that has no class 
loader. This includes built-in classes such as <code>java.lang.Object</code>, as well as 
classes that the host implementation may keep in, for example, a local file system.
<p><a name="28929"></a>
Given the <code>name</code> of a class, this method, like the <code>loadClass</code> method, either returns the <code>Class</code> object for the class or throws a <code>ClassNotFoundException</code>.<p>


<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javalang.doc12.html">Prev</a> | <a href="javalang.doc14.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -