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

📄 javalang.doc2.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.doc1.html">Prev</a> | <a href="javalang.doc3.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="14342"></a>
<center><h1>20.3  The Class  <code>java.lang.Class</code></h1></center>
<a name="10436"></a>
Instances of the class <code>Class</code> represent classes and interfaces in a way that can be 
manipulated by a running Java program. Every array also belongs to a class represented
by a <code>Class</code> object that is shared among all arrays with the same element 
type and number of dimensions.
<p><a name="10443"></a>
There is no public constructor for the class <code>Class</code>. The Java Virtual Machine automatically constructs <code>Class</code> objects as classes are loaded; such objects cannot be created by user programs.<p>
<pre><a name="13797"></a>public final class <code><b>Class</b></code> {
<a name="3333"></a>	public String <code><b>toString</b></code>();
<a name="1692"></a>	public String <code><b>getName</b></code>();
<a name="1696"></a>	public boolean <code><b>isInterface</b></code>();
<a name="1700"></a>	public Class <code><b>getSuperclass</b></code>();
<a name="1704"></a>	public Class[] <code><b>getInterfaces</b></code>();
<a name="36849"></a>	public Object <code><b>newInstance</b></code>()
<a name="52441"></a>		throws InstantiationException, IllegalAccessException;
<a name="52444"></a>	public ClassLoader <code><b>getClassLoader</b></code>();
<a name="52442"></a>	public static Class <code><b>forName</b></code>(String className)<br>
		throws ClassNotFoundException;
<a name="1686"></a>}
</pre><a name="1194"></a>
<p><font size=+1><strong>20.3.1   </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="23237"></a>
If this <code>Class</code> object represents a class (which may be a declared class or an array 
class), a string is returned consisting of the word <code>class</code>, a space, and the name of 
the class as returned by the <code>getName</code> method <a href="javalang.doc2.html#14964">(&#167;20.3.2)</a>. If this <code>Class</code> object represents
an interface, a string is returned consisting of the word <code>interface</code>, a space, 
and the name of the interface as returned by the <code>getName</code> method.
<p><a name="1197"></a>
In other words, this method returns a string equal to the value of:<p>
<pre><a name="1198"></a>(isInterface() ? "interface " : "class ") + getName()
</pre><a name="1202"></a>
Overrides the <code>toString</code> method of <code>Object</code> <a href="javalang.doc1.html#1152">(&#167;20.1.2)</a>.<p>
<a name="14964"></a>
<p><font size=+1><strong>20.3.2   </strong> <code>public String <code><b>getName</b></code>()</code></font>
<p>
<a name="14983"></a>
The fully qualified name of the class or interface represented by this <code>Class</code> object 
is returned as a <code>String</code>. For example:
<p><pre><a name="15000"></a><code>new Object().getClass().getName()
</code></pre><a name="15001"></a>
returns <code>"java.lang.Object"</code>.
<p><a name="31447"></a>
If this class object represents a class of arrays, then the name consists of the name of the element type in Java signature format, preceded by one or more "<code>[</code>" characters representing the depth of array nesting. For example:<p>
<pre><a name="31457"></a><code>(new Object[3]).getClass().getName()
</code></pre><a name="31458"></a>
returns <code>"[Ljava.lang.Object;"</code> and:
<p><pre><a name="31460"></a><code>(new int[3][4][5][6][7][8][9]).getClass().getName()
</code></pre><a name="31461"></a>
returns <code>"[[[[[[[I"</code>. The encoding of element type names is as follows:
<p><pre><a name="33413"></a>
B				byte
<a name="33414"></a>C				char
<a name="33415"></a>D				double
<a name="33416"></a>F				float
<a name="33417"></a>I				int
<a name="33418"></a>J				long
<a name="33419"></a>L<i>classname</i>;				class or interface
<a name="33423"></a>S				short
<a name="33424"></a>Z				boolean
</pre><a name="33432"></a>
A class or interface name <i>classname</i> is given in fully qualified form as shown in 
the example above. For a full description of type descriptors see the chapter on the 
format of class files in the <i>Java Virtual Machine Specification</i>.
<p><a name="14980"></a>
<p><font size=+1><strong>20.3.3   </strong> <code>public boolean <code><b>isInterface</b></code>()</code></font>
<p>
<a name="14984"></a>
If this <code>Class</code> object represents an interface, <code>true</code> is returned. If this <code>Class</code> object 
represents a class, <code>false</code> is returned.
<p><a name="14965"></a>
<p><font size=+1><strong>20.3.4   </strong> <code>public Class <code><b>getSuperclass</b></code>()</code></font>
<p>
<a name="15005"></a>
If this <code>Class</code> object represents any class other than the class <code>Object</code>, then the 
<code>Class</code> that represents the superclass of that class is returned. If this <code>Class</code> object 
is the one that represents the class <code>Object</code>, or if it represents an interface, <code>null</code> is 
returned. If this <code>Class</code> object represents an array class, then the <code>Class</code> that represents
class <code>Object</code> is returned.
<p><a name="34247"></a>
<p><font size=+1><strong>20.3.5   </strong> <code>public Class[] <code><b>getInterfaces</b></code>()</code></font>
<p>
<a name="15025"></a>
This method returns an array of objects that represent interfaces. The array may be 
empty.
<p><a name="15024"></a>
If this <code>Class</code> object represents a class, the array contains objects representing all interfaces directly implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the <code>implements</code> clause of the declaration of the class represented by this <code>Class</code> object. For example, given the class declaration:<p>
<pre><a name="15109"></a>class Shimmer implements FloorWax, DessertTopping { ... }
</pre><a name="15110"></a>
suppose the value of <code>s</code> is an instance of <code>Shimmer</code>; the value of the expression:
<p><pre><a name="15126"></a>s.getClass().getInterfaces()[0]
</pre><a name="15127"></a>
is the <code>Class</code> object that represents interface <code>FloorWax</code>; and the value of:
<p><pre><a name="15131"></a>s.getClass().getInterfaces()[1]
</pre><a name="15132"></a>
is the <code>Class</code> object that represents interface <code>DessertTopping</code>.
<p><a name="15124"></a>
If this <code>Class</code> object represents an interface, the array contains objects representing all interfaces directly extended by the interface-that is, the immediate superinterfaces of the interface. The order of the interface objects in the array corresponds to the order of the interface names in the <code>extends</code> clause of the declaration of the interface represented by this <code>Class</code> object.<p>
<a name="15088"></a>
<p><font size=+1><strong>20.3.6   </strong> <code>public Object <code><b>newInstance</b></code>()<br>throws InstantiationException, IllegalAccessException</code></font>
<p>
<a name="15139"></a>
This method creates and returns a new instance of the class represented by this 
<code>Class</code> object. This is done exactly as if by a class instance creation expression 
<a href="15.doc.html#41147">(&#167;15.8)</a> with an empty argument list; for example, if <code>t</code> is the <code>Class</code> object that represents
class <code>Thread</code>, then 	<code>t.newInstance()</code> does exactly the same thing as <code>new</code> 
<code>Thread()</code>. If evaluation of such a class instance creation expression would complete
abruptly, then the call to the <code>newInstance</code> method will complete abruptly 
for the same reason. See also <a href="11.doc.html#44328">&#167;11.5.1.2</a> for more on <code>InstantiationException</code>.
<p><a name="13804"></a>
<p><font size=+1><strong>20.3.7   </strong> <code>public ClassLoader <code><b>getClassLoader</b></code>()</code></font>
<p>
<a name="28541"></a>
This method returns a reference to the class loader <a href="javalang.doc13.html#14462">(&#167;20.14)</a> that loaded this class. 
If this class has no class loader, then <code>null</code> is returned.
<p><a name="28532"></a>
<p><font size=+1><strong>20.3.8   </strong> <code>public static Class <code><b>forName</b></code>(String className)<br>	throws ClassNotFoundException</code></font>
<p>
<a name="28533"></a>
Given the fully-qualified name of a class, this method attempts to locate, load, and 
link the class <a href="12.doc.html#44459">(&#167;12.2)</a>. If it succeeds, then a reference to the <code>Class</code> object for the 
class is returned. If it fails, then a <code>ClassNotFoundException</code> is thrown.
<p>

<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javalang.doc1.html">Prev</a> | <a href="javalang.doc3.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 + -