Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

java.lang.instrument.ClassDefinition Class in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

This class is used to bind together the supplied class and class file bytes in a single ClassDefinition object. These class provide methods to extract information about the type of class and class file bytes of an object. This class is a subclass of java.lang.Object class.

Class declaration:

public final class ClassDefinition
extends Object

Constructor:

ConstructorDescription 
ClassDefinition(Class<?> theClass, byte[] theClassFile)This constructor creates a new instance of  ClassDefinition class by binding the supplied class and class file bytes.

Methods:

MethodDescription
getDefinitionClass()This method is used to get the class type of this class
getDefinitionClassFile()This method Is used to get the array of bytes that contains the new class file.

Example 1: Java program to create new ClassDefinition object

Java




// Java program to create new ClassDefinition object
import java.lang.instrument.ClassDefinition;
  
// driver class
public class GFG {
    // demoClass
    static class demoClass {
        void msg() { System.out.println("GeeksForGeeks"); }
    }
    // main method
    public static void main(String[] args)
    {
        try {
  
            // creating demoClass object
            demoClass cls = new demoClass();
            
            // creating object of supplied class
            Class<?> theClass = cls.getClass();
            
            // creating array of class files
            byte[] classFiles = { 0 };
            
            // creating a new ClassDefinition object
            ClassDefinition classdefinition
                = new ClassDefinition(theClass, classFiles);
            System.out.println(
                "ClassDefinition object successfully created");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

ClassDefinition object successfully created

Example 2: Java program to illustrate ClassDefinition class methods

Java




// Java program to illustrate ClassDefinition class methods
import java.lang.instrument.ClassDefinition;
  
// driver class
public class GFG {
    
    // demoClass
    static class demoClass {
        void msg() { System.out.println("GeeksForGeeks"); }
    }
    
    // main method
    public static void main(String[] args)
    {
        try {
  
            // creating demoClass object
            demoClass cls = new demoClass();
            
            // creating object of supplied class
            Class<?> theClass = cls.getClass();
            
            // creating array of class files
            byte[] classFiles = { 0 };
            
            // creating a new ClassDefinition object
            ClassDefinition classdefinition
                = new ClassDefinition(theClass, classFiles);
            
            // printing the class
            System.out.println(
                classdefinition.getDefinitionClass());
            
            // printing array of bytes that contain class
            // files
            System.out.println(
                classdefinition.getDefinitionClassFile());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

class GFG$demoClass
[B@448139f0

My Personal Notes arrow_drop_up
Last Updated : 29 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials