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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:14:57+00:00 2026-06-01T18:14:57+00:00

Consider the following code: class ExtType extends MyType{}; class MyClass { MyType myField; public

  • 0

Consider the following code:

class ExtType extends MyType{};

class MyClass {
    MyType myField;

    public <T extends MyType> T foo(Class<T> clazz) {
        return (T)myField;
    }
}

Now I want to call foo method, I can do this two ways:

1 way:

(new MyClass()).foo(ExtType.class);

2 way:

(new MyClass()).<ExtType>foo(ExtType.class);

Interesting, that even the method is declared as parametrized, Eclipse doesn’t issue
any warning on 1 call.

Here is my question, in first code snippet, which of Ts is used in casting return value.
Is it a T from parameter or T from return value? Why if I don’t explicitly specify return type (as in “1 way”) no warning is issued?

  • 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-01T18:14:58+00:00Added an answer on June 1, 2026 at 6:14 pm

    Normally the parameter type is used but if there is a return type defined (like in way 2) the compiler would check that as well.

    The return type declaration would be necessary if there’s no parameter to get the type of T from, which then is called type inference. Thus you could even write:

    public <T extends MyType> T foo() {
        return (T)myField;
    }
    
    
    ExtType t = (new MyClass()).foo();
    

    In some cases you need to help the compiler and specify the type to be used thus getting code like (new MyClass()).<ExtType>foo(ExtType.class);. Note, however, that if you’d define different types, e.g. (new MyClass()).<MyType>foo(ExtType.class);, you’d get a compile time error, since the compiler now doesn’t know which one is used.

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

Sidebar

Related Questions

Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public
Consider the following code: public class Foo { private static final Object LOCK =
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following sample code: class MyClass { public long x; public void DoWork()
Consider following code public class City { public string Name { get { return
Let's consider the following code: public class MyPanel extends JPanel { private long secretInfo
Consider the following code: class A { public: void foo() { auto functor =
Consider the following code: class C { public: int operator-(int x) { return 3-x;

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.