< prev index next >

src/java.base/share/classes/java/lang/Enum.java

Print this page

258      * @throws IllegalArgumentException if the specified enum class has
259      *         no constant with the specified name, or the specified
260      *         class object does not represent an enum class
261      * @throws NullPointerException if {@code enumClass} or {@code name}
262      *         is null
263      * @since 1.5
264      */
265     public static <T extends Enum<T>> T valueOf(Class<T> enumClass,
266                                                 String name) {
267         T result = enumClass.enumConstantDirectory().get(name);
268         if (result != null)
269             return result;
270         if (name == null)
271             throw new NullPointerException("Name is null");
272         throw new IllegalArgumentException(
273             "No enum constant " + enumClass.getCanonicalName() + "." + name);
274     }
275 
276     /**
277      * enum classes cannot have finalize methods.




278      */
279     @SuppressWarnings("deprecation")

280     protected final void finalize() { }
281 
282     /**
283      * prevent default deserialization
284      */
285     @java.io.Serial
286     private void readObject(ObjectInputStream in) throws IOException,
287         ClassNotFoundException {
288         throw new InvalidObjectException("can't deserialize enum");
289     }
290 
291     @java.io.Serial
292     private void readObjectNoData() throws ObjectStreamException {
293         throw new InvalidObjectException("can't deserialize enum");
294     }
295 
296     /**
297      * A <a href="{@docRoot}/java.base/java/lang/constant/package-summary.html#nominal">nominal descriptor</a> for an
298      * {@code enum} constant.
299      *

258      * @throws IllegalArgumentException if the specified enum class has
259      *         no constant with the specified name, or the specified
260      *         class object does not represent an enum class
261      * @throws NullPointerException if {@code enumClass} or {@code name}
262      *         is null
263      * @since 1.5
264      */
265     public static <T extends Enum<T>> T valueOf(Class<T> enumClass,
266                                                 String name) {
267         T result = enumClass.enumConstantDirectory().get(name);
268         if (result != null)
269             return result;
270         if (name == null)
271             throw new NullPointerException("Name is null");
272         throw new IllegalArgumentException(
273             "No enum constant " + enumClass.getCanonicalName() + "." + name);
274     }
275 
276     /**
277      * enum classes cannot have finalize methods.
278      * 
279      * @deprecated Finalization has been deprecated for removal.  See
280      * {@link java.lang.Object#finalize} for background information and details
281      * about migration options.
282      */
283     @Deprecated(since="18", forRemoval=true)
284     @SuppressWarnings("removal")
285     protected final void finalize() { }
286 
287     /**
288      * prevent default deserialization
289      */
290     @java.io.Serial
291     private void readObject(ObjectInputStream in) throws IOException,
292         ClassNotFoundException {
293         throw new InvalidObjectException("can't deserialize enum");
294     }
295 
296     @java.io.Serial
297     private void readObjectNoData() throws ObjectStreamException {
298         throw new InvalidObjectException("can't deserialize enum");
299     }
300 
301     /**
302      * A <a href="{@docRoot}/java.base/java/lang/constant/package-summary.html#nominal">nominal descriptor</a> for an
303      * {@code enum} constant.
304      *
< prev index next >