Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:24:17+00:00 2026-06-12T02:24:17+00:00

In Java, is it safe to assume that getClass() called inside a constructor of

  • 0

In Java, is it safe to assume that getClass() called inside a constructor of a class used as a base class will provide information about the derived class, rather than the class of the base class?

It seems to work, but I guess that doesn’t necessarily mean it’s safe.
For example, if I have the following two classes:

public class Parent {
    public Parent() {
        System.out.println(getClass().getName());
    }
}

And:

public class Derived extends Parent {
    public Derived() {
        super();
    }

    public static void main(String... args) {
        new Derived();
    }
}

When I run the main() method in the Derived class it prints: Derived (which is what I was hoping). But can I rely on that behavior across JVMs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:24:19+00:00Added an answer on June 12, 2026 at 2:24 am

    getClass is one of Object‘s methods and returns the runtime class of this:

    Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.

    So yes, it will always return Derived.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it safe to assume that java.util.concurrent.CompletionService.take().isDone() will always return true? If so, why
Is there a guarantee that (the default, system) Java class loader doesn't attempt to
Is it safe to assume that: because scala.AnyRef defines toString() and hashCode() with a
I'm designing a tool using Java 6, that will read data from Medical Devices.
Is the standard Java 1.6 javax.xml.parsers.DocumentBuilder class thread safe? Is it safe to call
Is it safe to call write on Java FileOutputStream object form multiple threads? Will
Is a volatile int in Java thread-safe? That is, can it be safely read
Possible Duplicate: How thread-safe is enum in java? Let there be an enum class
When studying Java I learned that Strings were not safe for storing passwords ,
When does Java's Thread.sleep throw InterruptedException ? Is it safe to ignore it? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.