Optional of() method in Java with examples Last Updated : 30 Jul, 2019 Comments Improve Suggest changes Like Article Like Report The of() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. Syntax: public static <T> Optional<T> of(T value) Parameters: This method accepts value as parameter of type T to create an Optional instance with this value. Return value: This method returns an instance of this Optional class with the specified value of the specified type. Exception: This method throws NullPointerException if the specified value is null. Below programs illustrate of() method: Program 1: Java // Java program to demonstrate // Optional.of() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.of(9455); // print value System.out.println("Optional: " + op); } } Output: Optional: Optional[9455] Program 2: Java // Java program to demonstrate // Optional.of() method import java.util.*; public class GFG { public static void main(String[] args) { try { // create a Optional Optional<Integer> op = Optional.of(null); // print value System.out.println("Optional: " + op); } catch (Exception e) { System.out.println(e); } } } Output: java.lang.NullPointerException Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#of-T- Comment More infoAdvertise with us Next Article Optional of() method in Java with examples S ShubhamMaurya3 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Optional Practice Tags : Java Similar Reads Optional or() method in Java with examples The or() method of java.util.Optional class in Java is used to get this Optional instance if any value is present. If there is no value present in this Optional instance, then this method returns an Optional instance with the value generated from the specified supplier. Syntax: public Optional<T 2 min read OptionalInt of(int) method in Java with examples The of(int) method help us to get an OptionalInt object which contains a int value which is passed as a parameter to this method. Syntax: public static OptionalInt of(int value) Parameters: This method accepts a int value as parameter that will be set to the returned OptionalInt object. Return value 1 min read Optional get() method in Java with examples The get() method of java.util.Optional class in Java is used to get the value of this Optional instance. If there is no value present in this Optional instance, then this method throws NullPointerException. Syntax: public T get() Parameters: This method do not accept any parameter. Return value: Thi 2 min read Optional ofNullable() method in Java with examples The ofNullable() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. If the specified value is null, then this method returns an empty instance of the Optional class. Syntax: public static <T> Optional< 2 min read OptionalLong of(long) method in Java with examples The of(long) method help us to get an OptionalLong object which contains a long value which is passed as a parameter to this method. Syntax: public static OptionalLong of(long value) Parameters: This method accepts a long value as parameter that will be set to the returned OptionalLong object. Retur 1 min read Like