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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:52:43+00:00 2026-05-11T10:52:43+00:00

Suppose I’m using an interface with a generic type parameter interface Foo<T> { T

  • 0

Suppose I’m using an interface with a generic type parameter

interface Foo<T> {   T getOne();   void useOne(T t); } 

The intention is that the type T is abstract: it enforces a type constraint on implementations of Foo, but the client code doesn’t care exactly what T is.

This is no problem in the context of a generic method:

public <T> void doStuff(Foo<T> foo) {   T t = foo.getOne();   /* do stuff */   foo.useOne(t); } 

But suppose I want to break up the work of doStuff, saving some state in a class Bar. In this case, I seem to need to add the type parameter of Foo to Bar.

public class Bar<T> {   private Foo<T> foo;   private T t;    /* ... */    public void startStuff() {     t = foo.getOne();   }    public void finishStuff() {     foo.useOne(t);   } } 

This is kind of weird, since the type parameter T does not appear in the public interface of Bar (i.e., it is not included in any method parameter or return type). Is there a way to ‘quantify T away’? I.e., can I arrange for the parameter T to be hidden in the interface of Bar, as in the following?

public class Bar {   <T> { // foo and t have to use the same T     private Foo<T> foo;     private T t;   } // T is out of scope   ...  } 
  • 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. 2026-05-11T10:52:43+00:00Added an answer on May 11, 2026 at 10:52 am

    Your problem is similar to that solved by a ‘capture helper’, but I’m not sure it can be applied to your second example where two separate methods are used. Your first doStuff method could definitely be better written as public void doStuff(Foo<?> foo), since it works regardless of Foo type parameter. Then, the ‘capture helper’ pattern would be useful.


    Update: after tinkering a bit, extending the idea of Goetz’s capture helper, I came up with this. Inside, it looks a little messy; from the outside, you wouldn’t suspect a thing.

    public class Bar {   private final Helper<?> helper;   public Bar(Foo<?> foo) {     this.helper = Helper.create(foo);   }   public void startStuff() {     helper.startStuff();   }   public void finishStuff() {     helper.finishStuff();   }   private static class Helper<T> {     private final Foo<T> foo;     private T t;     private Helper(Foo<T> foo) {       this.foo = foo;     }     static <T> Helper<T> create(Foo<T> foo) {       return new Helper<T>(foo);     }     void startStuff() {       t = foo.getOne();     }     void finishStuff() {       foo.useOne(t);     }   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If GridView (rendered as a table) is within the div… May 12, 2026 at 5:58 am
  • Editorial Team
    Editorial Team added an answer The correct syntax for sinking these events in Javascript is… May 12, 2026 at 5:58 am
  • Editorial Team
    Editorial Team added an answer EDIT: While the below works, looking at the jQuery source… May 12, 2026 at 5:58 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose I have a class module clsMyClass with an object as a member variable.

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.