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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:28:05+00:00 2026-05-18T23:28:05+00:00

In the following test, TesterClass places a constraint on the relation between its two

  • 0

In the following test, TesterClass places a constraint on the relation between its two type parameters. The method func2() seems to break that constraint, and I expect it to cause a typing compilation error somewhere (on func2’s definition, or whenever the class is used with any second parameter other than String), but it doesn’t!

Furthermore, if I call func2 and save the result in an appropriately typed variable, the compilation fails (on the type of that variable). But doing the same and saving in a more general type (such as Object) succeeds, despite the fact the the function’s return type should have the same type in both cases (before the up-cast).

What’s going on here?

Thanks!

public class TestGenerics {
    public static class ParamedType<T> {}


    public class TesterClass<A extends ParamedType<B>, B> {
        public TesterClass<A, B> func() {
            return new TesterClass<A, B>();
        }

        public TesterClass<A, String> func2() {
            return new TesterClass<A, String>();
        }
    }

    public Object test() {
        // How can I use these type parameters? Doesn't .func2 now have an invalid return type?
        TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();

        //TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
        Object res = testClass.func2(); // Compiles
        return res;
    }
}

EDIT: This doesn’t compile in javac (versions reported below). I’m using Eclipse, and trying to find out what’s the compiler that’s actually running. Will update. May be a JDT (Eclipse compiler) bug.

I’ve opened a bug report for eclipse’s jdt:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503

  • 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-18T23:28:06+00:00Added an answer on May 18, 2026 at 11:28 pm

    Simple answer: it doesn’t compile, at least under javac 1.7:

    Test.java:10: type parameter A is not within its bound
        public TesterClass<A, String> func2() {
                           ^
      where A,B are type-variables:
        A extends ParamedType<B> declared in class Test.TesterClass
        B extends Object declared in class Test.TesterClass
    Test.java:11: type parameter A is not within its bound
            return new TesterClass<A, String>();
                                   ^
      where A,B are type-variables:
        A extends ParamedType<B> declared in class Test.TesterClass
        B extends Object declared in class Test.TesterClass
    2 errors
    

    You didn’t say what you were compiling it under – my guess is that your Java compiler has a bug in it.

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

Sidebar

Related Questions

No related questions found

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.