getclass() in Java

Learn via video courses
Topics Covered

Overview

getClass() in Java is a method of the Object class present in java.lang package. getClass() returns the runtime class of the object "this". This returned class object is locked by static synchronized method of the represented class.

Syntax of getClass() in Java

Method signature for the function is:

The syntax for calling the getClass() method is:

Here, "obj" is the name of the object upon which you would call the method.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Parameters of getClass() in Java

The getClass method does not take in any parameters.

Return value of getClass() in Java

The getClass method returns the runtime class of the object "this". For example, suppose we call getClass() on an object obj (as demonstrated in code snippet above), the return value of the getClass() will be the class of which this is an object of. "this" in this case is our object obj itself. The class object that is returned is locked by the static synchronized method of the represented class.

Exceptions of getClass() in Java

getClass() does not throw any exceptions.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

What is getClass() in Java?

The Object.getClass() method is called upon an instance of a class. If we have an object, we can call object.getClass() to determine the Class of the object. But we can not similarly use the Object.getClass() method upon abstract classes, or interfaces. We can not use getClass on primitive data types either.

Examples of getClass() in Java

Example 1: Calling lang.Object.getClass() method on Object class

Output::

We instantiate 3 objects of different classes, one of data type Object, String, and ArrayList each. Then we will call getClass() on each of these objects we created.

Example 2: Calling getClass() from a custom class

Outputs:

In the code snippet above, we instantiate an object "main" of our Main class. Then call the getClass method on the main object, to obtain its class name.

Note: The Object class is the superclass for all the classes in Java. Hence, every class can implement the getClass() method.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • getClass() method returns the runtime class of the object "this".
  • The object that getClass() returns, is locked by static synchronized method of the represented class.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more