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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:29:45+00:00 2026-05-29T15:29:45+00:00

Is there a way to typecast from Java.lang.Object to an instance of a custom

  • 0

Is there a way to typecast from Java.lang.Object to an instance of a custom defined class.

Essentially, I have created a Stack (java.util.Stack) and have pushed into it instances of type my_class. But when I pop out from the Stack, I receive an instance of type Java.lang.Object.

Do I have to create a constructor in my_class that can create my_class instances from Java.lang.Object ?


Generics is the best way to go. I am pretty new to Java, and without realizing about generics (same as in C++ STL), I have been doing a lot of typecasting like-

to convert to an integer: new Integer((Java.lang.Object).toString()).intValue()

Guess those days are gone now 🙂 Thanks for making my life easy.

  • 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-29T15:29:47+00:00Added an answer on May 29, 2026 at 3:29 pm

    You should write code using generics. For example instead of

    MyObject someObj = ...;
    Stack myStack = new Stack();
    myStack.push(someObj);
    someObj = myStack.pop(); // Error!
    

    you can let the stack know about the type of object within:

    MyObject someObj = ...;
    Stack<MyObject> myStack = new Stack<MyObject>();
    myStack.push(someObj);
    someObj = myStack.pop(); // Now this works!
    

    If for whatever reason this isn’t feasible, you can cast:

    MyObject someObj = ...;
    Stack myStack = new Stack();
    myStack.push(someObj);
    someObj = (MyObject) myStack.pop(); // This works too, but is considered very bad style
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a safer way to typecast data from a generic pointer.? More specifically
is there way how to get name ov event from Lambda expression like with
Is there way for a class to 'remove' methods that it has inherited? E.g.
Is there way to find out if Java is running on a platform which
Is there way in MySQL to create DATETIME from a given attribute of type
Is there a way to typecast String type to JLayeredPane ? Following is the
Is there way in MySQL to prevent someone from creating tables and/or databases with
I am receiving some JSON object from the server, and I want to 'typecast'
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or

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.