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

  • Home
  • SEARCH
  • 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 8190163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:31:47+00:00 2026-06-07T03:31:47+00:00

I have a simple class Apple extends from another simple class Fruit. At run-time,

  • 0

I have a simple class Apple extends from another simple class Fruit.

At run-time, I could use

Fruit fruit = new Apple();

fruit.getClass();

to get the actual type of fruit object, which is Apple.class.

I could also use fruit instanceof Apple, and fruit instanceof Fruit to verify if this fruit object is an instance of Apple or Fruit. Both of these 2 expressions return true, which is normal.

But is there a way to determine precisely the declared type of fruit identifier? Which in this case is Fruit.

  • 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-07T03:31:49+00:00Added an answer on June 7, 2026 at 3:31 am

    You’re actually asking a question about the variable declaration of fruit rather than the actual runtime type of the object (which is an Apple in this case).

    I think this is in general a bad idea: you just declared the variable and told the compiler that it is a Fruit, so why do you need to now need to find this out?

    Just to confuse matters even more, it’s worth noting that you can also have multiple variables with different declared types referencing the same object (which is still an Apple):

    Fruit fruit = new Apple(); // fruit declared as Fruit, but refers to an Apple
    Object thing = fruit;      // thing declared as Object, refers to the same Apple
    

    If you really want to find out the declared type, then you have a few options:

    • Make fruit an instance variable, and query the declared type using reflection.
    • Do some processing of the source code to find the variable declaration
    • Do some processing of the compiled bytecode to find the declaration type (although there is a possibility that an aggressive compiler might even optimise the compile time declaration away altogether, e.g. after realising that fruit can only ever be an Apple in this code)

    I think all of these are pretty ugly, so my general advice would be “don’t do it”.

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

Sidebar

Related Questions

I have an simple class that I get from a webservice. public class person
I have a simple class for use on JNI, which i need to export
I have a simple Class Hierarchy that I am trying to get to work
I have a simple class inheriting from WebControl as follow: public class Instrument :
I have created simple Java class to test file writes from applets: update appeared
I have simple class with width and height member fields which define number of
I have a simple class Student under namespace School. namespace XmlTestApp { public class
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have a simple class I'm serializing. [DataContract(Name = Test, Namespace = )] public
I have a simple class that contains some general information about the current web

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.