Writer getClass() method in Java with Examples

Last Updated : 17 Jun, 2026

The getClass() method of the Writer class is used to obtain the runtime class information of a Writer object. Since getClass() is inherited from the Object class, it can be called on any Java object to determine its actual class type during program execution.

  • Returns a Class object containing class metadata.
  • Useful for reflection, debugging, and type inspection.
  • Works with all subclasses of Writer, such as PrintWriter and OutputStreamWriter.
Java
import java.io.*;

public class GFG{
    public static void main(String[] args) {
        Writer writer = new PrintWriter(System.out);

        System.out.println(writer.getClass());
    }
}

Output
class java.io.PrintWriter

In this example, getClass() returns the runtime class of the object referenced by writer, which is PrintWriter.

Syntax

public final Class<?> getClass()

Method call

Writer writer = new PrintWriter(System.out);
Class<?> cls = writer.getClass();

  • Parameters: This method accepts does not accepts any parameter. 
  • Return Value: This method returns the Class details which is the parent Class of the Writer instance. 

Note: The Object class is the superclass for all the classes in Java. Hence, every class can implement the getClass() method.

Examples of the Writer getClass() method

The below methods illustrate the working of the getClass() method: 

Example 1: Using PrintWriter

Java
import java.io.*;

// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        try {
            // Create a Writer instance
            Writer writer = new PrintWriter(System.out);

            // Get the String
            // to be written in the stream
            String string = "GeeksForGeeks";

            // Write the string
            // to this writer using write() method
            writer.write(string);

            // Get Class details using getClass()
            System.out.println("Parent Class: "
                               + writer.getClass());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output
Parent Class: class java.io.PrintWriter

Explanation: In this example, a PrintWriter object is created and assigned to a Writer reference. When getClass() is called, it returns the actual runtime class of the object, which is java.io.PrintWriter. The result is then displayed on the console.

Example 2: Using OutputStreamWriter

Java
import java.io.*;

// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        try {
            // Create a Writer instance
            Writer writer
                = new OutputStreamWriter(System.out);

            // Get the String
            // to be written in the stream
            String string = "GFG";

            // Write the string
            // to this writer using write() method
            writer.write(string);

            // Get Class details using getClass()
            System.out.println("Parent Class: "
                               + writer.getClass());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output
Parent Class: class java.io.OutputStreamWriter

Explanation: In this example, a Writer reference points to an OutputStreamWriter object. Calling getClass() returns the runtime class information of the object, which is java.io.OutputStreamWriter. This demonstrates that getClass() identifies the actual object type rather than the reference type.

Advantages of getClass() Method in Java

  • Identifies the Runtime Class: Returns the actual class of an object during program execution.
  • Useful for Reflection: Helps inspect class information, methods, fields, and constructors dynamically.
  • Assists in Debugging: Makes it easier to determine the exact object type at runtime.
  • Supports Type Checking: Can be used to compare object types before performing operations.
  • Inherited by All Classes: Available to every Java object through the Object class.
  • Helps in Logging and Monitoring: Useful for displaying class names in logs and error messages.
  • Works with Polymorphism: Reveals the actual object type even when referenced by a superclass.
  • No Parameters Required: Simple to use without passing any arguments.
Comment