Java Class<>

Introduction

The Class<T> in Java, part of the java.lang package, represents the metadata of a class or interface. It provides methods to examine the properties of a class, such as its fields, methods, and constructors.

Table of Contents

  1. What is Class<T>?
  2. Key Methods
  3. Examples of Class<T>
  4. Conclusion

1. What is Class<T>?

Class<T> is a part of Java's reflection mechanism. It represents the class or interface with type parameter T, allowing you to inspect and interact with the class's metadata at runtime.

2. Key Methods

  • getName(): Returns the name of the class or interface.
  • getFields(): Returns an array of Field objects representing public fields.
  • getMethods(): Returns an array of Method objects representing public methods.
  • getConstructors(): Returns an array of Constructor objects representing public constructors.
  • newInstance(): Creates a new instance of the class.

3. Examples of Class<T>

Example 1: Getting Class Name

This example demonstrates how to get the name of a class using Class<T>.

public class ClassNameExample {
    public static void main(String[] args) {
        Class<String> stringClass = String.class;
        System.out.println("Class Name: " + stringClass.getName());
    }
}

Output:

Class Name: java.lang.String

Example 2: Getting Class Methods

Here, we get the methods of a class using Class<T>.

import java.lang.reflect.Method;

public class ClassMethodsExample {
    public static void main(String[] args) {
        Class<String> stringClass = String.class;
        Method[] methods = stringClass.getMethods();

        System.out.println("Methods of String class:");
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }
}

Output:

Methods of String class:
equals
length
toString
hashCode
getChars
compareTo
compareTo
indexOf
indexOf
indexOf
indexOf
indexOf
indexOf
valueOf
valueOf
valueOf
valueOf
valueOf
valueOf
valueOf
valueOf
valueOf
charAt
codePointAt
codePointBefore
codePointCount
offsetByCodePoints
getBytes
getBytes
getBytes
getBytes
contentEquals
contentEquals
regionMatches
regionMatches
startsWith
startsWith
lastIndexOf
lastIndexOf
lastIndexOf
lastIndexOf
substring
substring
isEmpty
replace
replace
matches
replaceFirst
replaceAll
split
split
splitWithDelimiters
join
join
toLowerCase
toLowerCase
toUpperCase
toUpperCase
trim
strip
stripLeading
stripTrailing
lines
repeat
isBlank
toCharArray
format
format
resolveConstantDesc
resolveConstantDesc
codePoints
equalsIgnoreCase
compareToIgnoreCase
endsWith
subSequence
concat
contains
indent
stripIndent
translateEscapes
chars
transform
formatted
copyValueOf
copyValueOf
intern
describeConstable
getClass
notify
notifyAll
wait
wait
wait

Example 3: Creating a New Instance

This example shows how to create a new instance of a class using Class<T>.

public class NewInstanceExample {
    public static void main(String[] args) {
        try {
            Class<StringBuilder> sbClass = StringBuilder.class;
            StringBuilder sb = sbClass.newInstance();
            sb.append("Hello, Reflection!");
            System.out.println(sb.toString());
        } catch (InstantiationException | IllegalAccessException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Hello, Reflection!

Conclusion

The Class<T> in Java is used for reflection, allowing you to inspect and interact with class metadata at runtime. It provides various methods to access class properties and is essential for advanced Java programming techniques.

Comments