Java Snippet Class

In Java, Snippet is a class that belongs to jdk.jshell module and package. It was introduced in Java 9. It is used to represent a snippet (piece) of Java source code as passed to JShell.eval(). It is created by the JShell instance, for this reason it is associated only with JShell.

An instance of the Snippet class (including its subclasses) is immutable that makes it thread safe. Access to any of its methods will always return the same result.

Nested Subclasses of the Snippet Class

The class provides the following three subclasses:

Snippet.Kind: It describes the general kind of snippet.

Snippet.Status: It describes the current state of a Snippet.

Snippet.Subkind: It The detailed variety of a snippet.

Snippet Class Methods

The class provides the following methods:

MethodsDescription
id()It returns a unique identifier of the snippet. Note that no two active snippets have the same ID.
kind()It returns the Snippet.Kind of the snippet. It indicates the subclass of the Snippet.
source()It returns the source code of the snippet.
subKind()It returns the Snippet.Subkind of the snippet. It is useful for feedback to users.
toString()It returns a string representation of the object.

It overrides the toString() method of the Object class.

Apart from these methods, it also provides the following methods of the Object class.

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Note: Ensure that JDK 9 or later version is installed in your system.