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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:05:13+00:00 2026-06-15T22:05:13+00:00

Problem: I want to create a generic function which will return me strogly typed

  • 0

Problem:

I want to create a generic function which will return me strogly typed object.

Function :

public <T > T GetPet(AnimalKingdom allAnimals,int id) {
    return (T) allAnimals.getAnimalsManager().findAnimalById(id);

}

The above function will return a strongly typed object or throw error.

Usage :

GetPet<Tiger>(thisZoo,tigersId).Roar();

Coming from C# background. Googled for the same but was not able to find a solution, it seems that I need to pass the generic type in function for it to work.

How can the above scenario implemented in java.

  • 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-15T22:05:15+00:00Added an answer on June 15, 2026 at 10:05 pm

    In Java, you explicitly specify the type arguments of the generic method before the method name. For example:

    class Example {
        public static <T> T getPet(AnimalKingdom allAnimals, int id) {
            return (T)allAnimals.getAnimalsManager().findAnimalById(id);
        }
    }
    

    Then to invoke:

    Example.<Tiger>getPet(thisZoo, tigersId).roar();
    

    Alternatively, you can use type inferencing:

    Tiger tiger = Example.getPet(thisZoo, tigersId);
    tiger.roar();
    

    By the way, your getPet method is not very safe because there is no runtime check performed to ensure the object returned is actually an instance of Tiger. In fact, the Java compiler gives a warning on this line:

    return (T)allAnimals.getAnimalsManager().findAnimalById(id);
    

    The reason is because the cast to (T) is unchecked due to type erasure.

    To strengthen your code, I suggest the following change:

    class Example {
        public static <T> T getPet(Class<T> clazz, AnimalKingdom allAnimals, int id) {
            return clazz.cast(allAnimals.getAnimalsManager().findAnimalById(id));
        }
    }
    

    Then to invoke:

    Example.getPet(Tiger.class, thisZoo, tigersId).roar();
    

    The benefit of passing in the class object (Tiger.class) is that:

    1. It provides the compiler the type to use for <T> (again via type inferencing).
    2. You can add your own explicit runtime type check by calling the Class.cast method. The Java compiler warning goes away, and type safety is restored. For example, if findAnimalById mistakenly returns an instance of Bear when you expected Tiger, you will get a ClassCastException.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with moving animation. I want to create animation which start
The problem is this: I want a generic function that has an out-parameter of
I want to create a plugin extensibility mechanism for a very generic application which
Problem I want to create an application that can be extended somehow by programmers.
i'm having problem when i want to create mobile optimization in my site,i'm read
I want to create fast loading reports like ActiveReports or DevExpress but the problem
I want to create windows application in c# 2008. The problem is I don't
I want to create a crawler with C#. The problem is that some websites
Here is the problem details: 1) I want to create dynamic (ip based) download
I want to create such layout: +-----------container-----------+ | +-----fixed-height-div-------+| | +-------fixed-height-div-----+| | +-fill-height-scrollable-div-+| | +-fixed-height-bottom-div-+| +---------end-container-------+ The problem

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.