Writer getClass() method in Java with Examples
Last Updated :
29 May, 2023
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
Writer writer = new PrintWriter(System.out);
String string = "GeeksForGeeks" ;
writer.write(string);
System.out.println( "Parent Class: "
+ writer.getClass());
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output
Parent Class: class java.io.PrintWriter
Example 2
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
Writer writer
= new OutputStreamWriter(System.out);
String string = "GFG" ;
writer.write(string);
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
Please Login to comment...