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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:46:14+00:00 2026-06-13T12:46:14+00:00

I have a stack of Objects, in this stack I push ClassA and ClassB

  • 0

I have a stack of Objects, in this stack I push ClassA and ClassB objects.
I have a method which must return objects from this stack

public Object method(Stack s){
    ClassA a = new ClassA();
    stack.push(a);
    ClassB b = new ClassB();
    stack.push(b);
    while(stack has two elements) stack.pop();
    return stack.pop()// I return the last element
}

the problem is: when I call this method instanceof doesn’t work, it can’t tell anymore ClassA from ClassB

Object o = method(s);
if ( o instanceof ClassA){
    //do something
} else if (o instanceof ClassB) {
    //do something else
}

Inside method(Stack s) instanceof works, outside doesn’t, but the toString() method works fine, it return the proper String for each class.

  • 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-13T12:46:14+00:00Added an answer on June 13, 2026 at 12:46 pm

    There are a few scenarios where your introspection code may not work as you expect:

    • If o is null, the instanceof operator returns false. The null value is not an instance of any type … according to the JLS.

    • If ClassB is a subtype of classA, then o instanceof ClassA will return true for an instance of ClassB.

    • If you are doing something complicated with classloaders, it is possible to load the same “.class” file with different class loaders. If you do that, you will run into the problems that the two “copies” of the class actually have different types … and instanceof will return false. (This behaviour is also specified in the JLS.)

    The last case can be particularly confusing, because when you look at the names of the two classes they will be the same. However class1.equals(class2) or class1 == class2 will give you the definitive answer about whether two classes are actually the same.

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

Sidebar

Related Questions

I have constructed a generic method which deserialiazes an object in the following manner:
I have SendingController which push to nav stack SendingDeatilsController (which one has a TableView).
I have this list of custom class objects: List<URL> URLs = new List<URL>(); and
I have some code below. This code is a basic push/pop stack class that
I have a stack panel for which AllowDrop property is true and I have
I have a stack which contains some integer data. I want to find out
I have the a class Foo like this: class Foo { public int id{get;set;}
I have a UITableView (on a navigation controller stack) which is showing details for
I have a view controller that i'm trying to push onto the navigation stack.
Do class objects declared on the stack have the same lifetime as other stack

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.