Skip to content
Related Articles

Related Articles

Improve Article
java.lang.instrument.ClassDefinition Class in Java
  • Last Updated : 29 Mar, 2021

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :