Class: getName() : Class « java.lang « Java by API






Class: getName()

   
/*
 * Output: 
x is object of type: Class1
y is object of type: Class2
y's superclass is Class1

 *  
 */

class Class1 {
  int a;

  float b;
}

class Class2 extends Class1 {
  double c;
}

public class MainClass {
  public static void main(String args[]) {
    Class1 x = new Class1();
    Class2 y = new Class2();
    Class clObj;

    clObj = x.getClass(); // get Class reference
    System.out.println("x is object of type: " + clObj.getName());

    clObj = y.getClass(); // get Class reference
    System.out.println("y is object of type: " + clObj.getName());
    clObj = clObj.getSuperclass();
    System.out.println("y's superclass is " + clObj.getName());
  }
}


           
         
    
    
  








Related examples in the same category

1.Class: forName(String classDec)
2.Class: Annotation[] getAnnotations()
3.Class: getCanonicalName()
4.Class: getClasses()
5.Class: getComponentType()
6.Class: getConstructors()
7.Class: getConstructor(Class...)
8.Class: getDeclaringClass()
9.Class: getDeclaredConstructors()
10.Class: getDeclaredFields()
11.Class: getDeclaredMethods()
12.Class: getDeclaredMethod(String name, Class... parameterTypes)
13.Class: getEnumConstants()
14.Class: getFields()
15.Class: getGenericInterfaces()
16.Class: getGenericSuperclass()
17.Class: getInterfaces()
18.Class: Method getMethod(String name, Class... parameterTypes)
19.Class: getMethods()
20.Class: getModifiers()
21.Class: getPackage()
22.Class: getProtectionDomain()
23.Class: getResource(String name) (absolute from the classpath)
24.Class: getResource(String name) (relative to the class location)
25.Class: getResourceAsStream(String name)
26.Class: getSuperclass()
27.Class: getTypeParameters()
28.Class: isArray()
29.Class: isAssignableFrom(Class cls)
30.Class: isEnum()
31.Class: isInstance(Object obj)
32.Class: isInterface()
33.Class: newInstance()