Java Program to Determine the class of an object

Configurare noua (How To)

Situatie

In this example, we will learn to determine the class of an object in Java using the getClass() method.

Solutie

Pasi de urmat

class Test1 {
// first class
}

class Test2 {
// second class
}

class Main {
public static void main(String[] args) {
// create objects
Test1 obj1 = new Test1();
Test2 obj2 = new Test2();

// get the class of the object obj1
System.out.print(“The class of obj1 is: “);
System.out.println(obj1.getClass());

// get the class of the object obj2
System.out.print(“The class of obj2 is: “);
System.out.println(obj2.getClass());
}
}

Output

The class of obj1 is: class Test1
The class of obj2 is: class Test2

Tip solutie

Permanent

Voteaza

(7 din 14 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?