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 3232336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:08:57+00:00 2026-05-17T17:08:57+00:00

In the following code, the type of x is I (although x also implements

  • 0

In the following code, the type of x is I (although x also implements J but thats not known at compile time) so why is it that the code at (1) doesn’t result in a compile time error.
Because at compile time only the type of the reference is considered.

public class MyClass {
    public static void main(String[] args) {
        I x = new D();
        if (x instanceof J) //(1)
            System.out.println("J");
    }
}

interface I {}

interface J {}

class C implements I {}

class D extends C implements J {}
  • 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-05-17T17:08:57+00:00Added an answer on May 17, 2026 at 5:08 pm

    instanceof is used used for runtime determination of an object’s type. You are trying to determine if x is really an object of type J when the program is running, so it compiles.

    Were you thinking it should result in a compile-time error because you think the compiler does not know x‘s type?

    Edit

    As Kirk Woll has commented (thanks Kirk Woll!), if you were checking if x is an instanceof a concrete class, and the compiler can determine x‘s type, then you will get an error at compile time.

    From the Java Language Specification:

    If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.

    As an example of this:

    import java.io.Serializable;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    
    class SerializableClass implements Serializable
    {
       private writeObject(ObjectOutputStream out) {}
       private readObject(ObjectInputStream in) {}
    }
    
    public class DerivedSerializableClass extends SerializableClass
    {
       public static void main(String[] args)
       {
          DerivedSerializableClass dsc = new DerivedSerializableClass();
    
          if (dsc instanceof DerivedSerializableClass) {} // fine
          if (dsc instanceof Serializable) {} // fine because check is done at runtime
          if (dsc instanceof String) {} // error because compiler knows dsc has no derivation from String in the hierarchy
    
          Object o = (Object)dsc;
          if (o instanceof DerivedSerializableClass) {} // fine because you made it Object, so runtime determination is necessary
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code that needs to evaluate its type although Im not
The following code throws an compile-time error like Cannot convert type 'string' to 'int'
I have the following code that won't compile and although there is a way
The following code yields an error error: ‘struct Foo’ is not a valid type
The following piece of C# code does not compile: public class A { public
I have the following snippet of code, that although entirely trivial, illustrates a pattern
I have the following code: Type type = typeof(T); foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public
I am successfully instantiating/automating Visual Studio using the following code: System.Type t = System.Type.GetTypeFromProgID(VisualStudio.DTE.9.0);
the following code specifies a type MyBase64Binary which is derived from a base class
I have the following code: $.ajax({ type: 'GET', url: 'index.php?route=checkout/onepagecheckout/getpaypaldata', dataType: 'json', success: function(json)

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.