Home » Java programming language

Java ObjectStreamClass forClass() Method with Example

ObjectStreamClass Class forClass() method: Here, we are going to learn about the forClass() method of ObjectStreamClass Class with its syntax and example.
Submitted by Preeti Jain, on April 12, 2020

ObjectStreamClass Class forClass() method

  • forClass() method is available in java.io package.
  • forClass() method is used to return the Class in the local virtual machine that this version is associated with.
  • forClass() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • forClass() method does not throw an exception at the time of returning Class.

Syntax:

    public Class forClass();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Class, it returns the class instance that this descriptor denotes or may returns null when there is no desired local class.

Example:

// Java program to demonstrate the example 
// of Class forClass() method of ObjectStreamClass

import java.io.*;

public class ForClass {
 public static void main(String[] args) {
  // Instantiates two ObjectStreamClass for Long
  // and Calendar
  ObjectStreamClass o_stm1 = ObjectStreamClass.lookup(Long.class);
  ObjectStreamClass o_stm2 = ObjectStreamClass.lookup(String.class);

  // By using forClass() method is to return
  // the class that the version is linked to
  Class cl1 = o_stm1.forClass();
  Class cl2 = o_stm2.forClass();

  // Display cl1 and cl2
  System.out.println("o_stm1.forClass(): " + cl1);
  System.out.println("o_stm2.forClass(): " + cl2);
 }
}

Output

o_stm1.forClass(): class java.lang.Long
o_stm2.forClass(): class java.lang.String 






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.