Thursday, September 4, 2025
HomeLanguagesJavaWriter getClass() method in Java with Examples

Writer getClass() method in Java with Examples

The getClass() method of Writer Class in Java is used to get the parent Class of this Writer instance. This method does not accept any parameter and returns the required Class details. 

Syntax of getClass() method

public final Class String getClass()

Calling of the getClass() method

object_name.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

Java




// Java program to demonstrate
// Writer getClass() method
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

Example 2

Java




// Java program to demonstrate
// Writer getClass() method
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
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS