Open In App

Writer getClass() method in Java with Examples

Last Updated : 29 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads