Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor. Returns: a newly allocated instance of the class represented by this object.
Throws: IllegalAccessException - if the class or its nullary constructor is not accessible. InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason. ExceptionInInitializerError - if the initialization provoked by this method fails. SecurityException - If a security manager, s , is present and any of the following conditions is met: invocation of s.
PUBLIC denies creation of new instances of this class the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException.
It returns false otherwise. Specifically, if this Class object represents a declared class, this method returns true if the specified Object argument is an instance of the represented class or of any of its subclasses ; it returns false otherwise.
If this Class object represents an array class, this method returns true if the specified Object argument can be converted to an object of the array class by an identity conversion or by a widening reference conversion; it returns false otherwise. If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise.
If this Class object represents a primitive type, this method returns false. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false. Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion.
See The Java Language Specification , sections 5. Parameters: cls - the Class object to be checked Returns: the boolean value indicating whether objects of the type cls can be assigned to objects of this class Throws: NullPointerException - if the specified Class parameter is null.
Since: JDK1. Returns: true if this object represents an interface; false otherwise. Returns: true if this object represents an array class; false otherwise. There are nine predefined Class objects to represent the eight primitive types and void.
These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean , byte , char , short , int , long , float , and double. These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.
Returns: true if and only if this class represents a primitive type Since: JDK1. TYPE , Character. TYPE , Byte. TYPE , Short. TYPE , Integer. TYPE , Long. TYPE , Float. TYPE , Double. TYPE , Void. Note that if this method returns true, isInterface would also return true, as all annotation types are also interfaces. Returns: true if this class object represents an annotation type; false otherwise Since: 1. Returns: true if and only if this class is a synthetic class as defined by the Java Language Specification.
Since: 1. If this class object represents a primitive type or void, then the name returned is a String equal to the Java language keyword corresponding to the primitive type or void. If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more ' [ ' characters representing the depth of the array nesting.
The encoding of element type names is as follows: Element Type Encoding boolean Z byte B char C class or interface L classname ; double D float F int I long J short S The class or interface name classname is the binary name of the class specified above. Examples: String. String" byte. Object;" new int[3][4][5][6][7][8][9]. 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 calls the 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. Returns an array of length 0 if the underlying generic declaration declares no type variables. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.
If this object represents an array class then the Class object representing the Object class is returned. Returns: the superclass of the class represented by this object. If the superclass is a parameterized type, the Type object returned must accurately reflect the actual type parameters used in the source code. The parameterized type representing the superclass is created if it had not been created before.
See the declaration of ParameterizedType for the semantics of the creation process for parameterized types. The class loader of this class is used to find the package. Null is returned if no package object was created by the class loader of this class. Packages have attributes for versions and specifications only if the information was defined in the manifests that accompany the classes, and if the class loader created the package instance with the attributes from the manifest.
Returns: the package of the class, or null if no package information is available from the archive or codebase. If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the class.
The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object. If this object represents an interface, the array contains objects representing all interfaces extended by the interface. The order of the interface objects in the array corresponds to the order of the interface names in the extends clause of the declaration of the interface represented by this object.
If this object represents a class or interface that implements no interfaces, the method returns an array of length 0. If this object represents a primitive type or void, the method returns an array of length 0. Returns: an array of interfaces implemented by this class. If a superinterface is a parameterized type, the Type object returned for it must accurately reflect the actual type parameters used in the source code.
The parameterized type representing each superinterface is created if it had not been created before. In the case of an array class, the interfaces Cloneable and Serializable are returned in that order. If this object represents an interface, the array contains objects representing all interfaces directly extended by the interface.
If this class does not represent an array class this method returns null. Returns: the Class representing the component type of this class if this class is an array Since: JDK1. The modifiers consist of the Java Virtual Machine's constants for public , protected , private , final , static , abstract and interface ; they should be decoded using the methods of class Modifier. If the underlying class is an array class, then its public , private and protected modifiers are the same as those of its component type.
If this Class represents a primitive type or void, its public modifier is always true , and its protected and private modifiers are always false.
If this object represents an array class, a primitive type or void, then its final modifier is always true and its interface modifier is always false. The values of its other modifiers are not determined by this specification.
Returns: the int representing the modifiers for this class Since: JDK1. When you run a method that calls Class. You can receive unexpected results if you try to locate a class with an incorrect resolver. For example, if a class X in schema X requests a class Y in schema Y to look up class Z , you will experience an error if you expected the resolver of class X to be used.
Because class Y is performing the lookup, the resolver associated with class Y is used to locate class Z. In summary, if the class exists in another schema and you specified different resolvers for different classes, as would happen by default if they are in different schemas, you may not find the class. Supply the ClassLoader instance in the Class.
Supply the class and the schema it resides in to the classForNameAndSchema method. Another unexpected behavior can occur if system classes invoke Class. Supply ClassLoader in Class. Supply Class and Schema Names to lookupClass. Supply Class and Schema Names when Serializing. Oracle Database uses resolvers for locating classes within schemas. Every class has a specified resolver associated with it, and each class can have a different resolver associated with it.
As a result, the locating of classes is dependent on the definition of the associated resolver. The ClassLoader instance knows which resolver to use, based on the class that is specified. When you supply a ClassLoader instance to Class. The syntax of this variant of Class. The following examples show how to supply the class loader of either the current class instance or the calling class instance.
This class name is specified as the string parameter. Return Value: This method returns the instance of this Class with the specified class name. Exception: This method throws following Exceptions: Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. LinkageError: if the linkage fails ExceptionInInitializerError: if the initialization provoked by this method fails ClassNotFoundException: if the class cannot be located Below programs demonstrate the forName method.
String" ; System. Integer" ; System. Skip to content. This site uses Akismet to reduce spam. Learn how your comment data is processed. We publish useful tutorials on Java, J2EE and all latest frameworks. Connect with us on Facebook Privacy Policy Sitemap. Leave a Reply Cancel reply Your email address will not be published.
0コメント