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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:53:56+00:00 2026-05-30T04:53:56+00:00

Consider the following constructor for the class Foo (which for the sake of clarity

  • 0

Consider the following constructor for the class Foo (which for the sake of clarity is not a generic class):

public <T> Foo(T obj) { }

This is valid syntax for constructors, just like with normal generic methods.

But what is the use of this syntax? Typically generic methods provide type safety for their return type, and can benefit from type inference by the compiler. For example:

Pair<String, Integer> stringInt = Pair.of("asfd", 1234);

But a call to a constructor always returns an instance of its declaring class, so its type parameters have no effect on the return type. The constructor above could just be replaced with its erasure:

public Foo(Object obj) { }

Of course generics aren’t only about type safety for return types. The constructor might just want to constrain the type of argument(s) being passed in. However, the above reasoning still applies for a bounded type parameter:

public <N extends Number> Foo(N number) { }

public Foo(Number number) { } //same thing

Even nested type parameters with bounds are handled using wildcards:

public <N extends Number, L extends List<N>> Foo(L numList) { }

public Foo(List<? extends Number> numList) { } //same thing

So what is a legitimate use case for having a generic constructor?

  • 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-30T04:53:58+00:00Added an answer on May 30, 2026 at 4:53 am

    Here’s a possible one, adapted from functional programming. Suppose we have a Stream type that has some internal state, repeatedly yielding new elements until it returns null. The outside callers don’t care what the internal state type of the stream type is, so you might get something like

    class Stream<E> {
      <S> Stream(S initialState, StepFunction<E, S> stepFun) {
        ...
      }
    }
    

    without the recipient having to know what the internal state type is.

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

Sidebar

Related Questions

Consider the following template class class MyClassInterface { public: virtual double foo(double) = 0;
Original Question Consider the following scenario: public abstract class Foo { public string Name
Consider the following, simple class constructor. (note that I am obviously not including all
Consider the following: class A { public: const int c; // must not be
Consider the following basic class: public class ConstructorExample { public ConstructorExample(){ System.out.println(Constructor called.); }
Okay, consider the following classes: class Object { public: // Constructor Object() : [Initialization
Please consider the following: class CMyClass { public: CMyClass() { printf( "Constructor\n" ); }
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following class: class A { const int arr[2]; public: A() { }

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.