Sobes.tech
Intern — Junior

What properties or elements of a class can be viewed or obtained from its definition?

sobes.tech AI

Answer from AI

From class definition in Java, you can obtain the following properties and elements:

  • Fields (class variables) — their names, types, and access modifiers.
  • Methods — names, return types, parameters, access modifiers.
  • Constructors — their parameters and modifiers.
  • Nested classes and interfaces — declared inside the class.
  • Class modifiers — such as public, abstract, final.
  • Inheritance and implemented interfaces — specified via extends and implements.

Using reflection, you can get this data at runtime:

Class<?> clazz = MyClass.class;

// Get fields
Field[] fields = clazz.getDeclaredFields();

// Get methods
Method[] methods = clazz.getDeclaredMethods();

// Get constructors
Constructor<?>[] constructors = clazz.getDeclaredConstructors();

// Get modifiers
int modifiers = clazz.getModifiers();

// Get superclass
Class<?> superclass = clazz.getSuperclass();

// Get interfaces
Class<?>[] interfaces = clazz.getInterfaces();

Thus, from class definition, you can get complete information about its structure and behavior.