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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:53:57+00:00 2026-05-15T17:53:57+00:00

class Response<T> { … } Response response = new Response(); The above code compiles.

  • 0
class Response<T>
{ ... }

Response response = new Response();

The above code compiles. I don’t understand what’s implied though. Shouldn’t the compiler require a type specification for ‘T’? e.g. something like the following:

Response<String> response = new Response<String>(); 
  • 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-15T17:53:58+00:00Added an answer on May 15, 2026 at 5:53 pm

    Technically, it should/ought to. However, for backwards compatibility with Java 5, this is not done and so a generic parameter is not required. And since generics are implemented by erasure anyway, the bytecode emitted is identical regardless of whether you declare the parameters or not – all you’re missing out on is some compile-time checks.

    (Note that if you call methods on your response object, the compiler will warn you about using “raw types”, meaning that you’re using a generic class in a non-generic way so it can’t enforce any of the constraints for you.)

    Edit: Regarding backwards compatibility, it’s a balancing act, and Sun have clearly sacrificed some aspects in order to improve/maintain others. Breaking backwards compatibility would be quite a big deal – it would mean that migrating to the latest version of Java would be a non-trivial project, and would create even more resistance within businesses to upgrading.

    The big decision here was to implement generics via erasure, such that they’re a “compile-time only” construct and the generated bytecode is identical to the previous version. This has the advantage that e.g. java.util.HashMap in 1.5 can still be accessed by code written in 1.4 (and of course this advantage extends to your own classes too). However, there are lots of view, especially from those used to generics in other languages who want to use similar techniques, that this was not the best decision and has crippled the usefulness of generics. I’m not going to weigh in on that here.

    As for whether it subverts the checks the compiler wants to enforce; I don’t think it’s quite as bad as you think. Yes, you can write code such that the compiler doesn’t do any generics checks, and such that you can deliberately subvert the intended semantics. However, the compile-time checks aren’t meant to be some sort of security feature, they’re simply there to help you, as a form of static analysis that will pick up certain classes of error. If you want to subvert them, feel free to do so. But if you write your generic classes properly, then you’ll get the compile-time checks that you desire.

    Especially since the compiler (can) give you warnings about raw types, there’s a clear upgrade path from 1.4 to 5. Upgrade your JDK – your old code still compiles albeit with warnings. Then use these warnings to chase down violations, and generify your old code as and when needed. In my opinion, that’s much better than simply refusing to compile the old (presumably functional!) code until every statement has had appropriate generics added.

    Most IDEs will let you classify the severity of different warning types, such that if you’re developing a Java 5 application from scratch, you can tell it to treat all raw type warnings as full-on stop-the-build errors.

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

Sidebar

Ask A Question

Stats

  • Questions 484k
  • Answers 484k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't do this in SQL. Sensibly that is. This… May 16, 2026 at 7:27 am
  • Editorial Team
    Editorial Team added an answer It is relatively easy to write one. May 16, 2026 at 7:27 am
  • Editorial Team
    Editorial Team added an answer get is just a method that returns a value, so… May 16, 2026 at 7:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.