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

  • Home
  • SEARCH
  • 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 8458535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:06:25+00:00 2026-06-10T13:06:25+00:00

Studying Java, I’ve come across generic methods . public <T> void foo(T variable) {

  • 0

Studying Java, I’ve come across generic methods.

public <T> void foo(T variable) { }

That is, a method which takes a parameter with an undecided type (á la PHP?). I’m however unable to see how this would be a good solution – especially since I’ve come to fall in love with a strongly typed languages after coming from a loose ones.

Is there any reason to use generic methods? If so, when?

  • 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-10T13:06:27+00:00Added an answer on June 10, 2026 at 1:06 pm

    Generics, among other things, give you a way to provide a template — i.e. you want to do the same thing, and the only difference is the type.

    For example, look at the List API, you will see the methods

    add(E e)

    For every list of the same type you declare, the only thing different about the add method is the type of the thing going into the list. This is a prime example of where generics are useful. (Before generics were introduced to Java, you would declare a list, and you could add anything to the list, but you would have to cast the object when you retrieved it)

    More specifically, you might want 2 ArrayList instances, one that takes type1 and one that takes type2. The list code for add is going to do the same thing, execute the same code, for each list (since the two lists are both ArrayList instances), right? So the only thing different is what’s in the lists.

    (As @michael points out, add isn’t a true example of a generic method, but there are true generic methods in the API linked, and the concept is the same)

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

Sidebar

Related Questions

Currently I am studying for my Java test. Whist studying I've come across a
I'm studying for a programming interview in Java which may involve database access. Is
I recently start studying JAVA, and I find this code: public class Dublicate{ public
While studying the Collection API, we find that some methods ( add , remove
Possible Duplicate: Instantiating a generic class in Java I am studying Generics Java because
I am studying servlets I read that servlets are java programs but there are
I am currently studying Java to improve myself. I have a program which has
i recently started learning Java. I was studying Vectors and I came across various
When studying Java I learned that Strings were not safe for storing passwords ,
I am studying java, and I remember reading somewhere that java objects, had some

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.