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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:11:08+00:00 2026-06-15T14:11:08+00:00

In Java I’m looking for a generic template that means arrays of a given

  • 0

In Java I’m looking for a generic template that means arrays of a given type (say Foo), will allow instance method calls upon the array. Behind-the-scenes this would translate to iterating over all Foo instances in the array, and calling the instance method upon each one.

Perhaps some code will demonstrate this point better:

public class Foo{

   public Foo(){}

   public void Method1(){}

}

So you have:

Foo foo = new Foo();
foo.Method1();

But could you make some generic template for your custom types that inherently made this kind of thing possible:

Foo[] foos = new Foo[]{new Foo(),new Foo(), new Foo()};
foos.Method1();

Which is essentially syntactic sugar for:

foreach(Foo f : foos){
  f.Method1();
}

My motivation is so that someone can use varargs such that:

someHelper(fooInstance1,fooInstance2).Method1()

Where someHelper() returns the Foo[].

If each Method1() invocation returned a value, it would be even better if this was wrapped in to an array of return values (where ReturnVals.size == Foos.size).

In the worst-case I’d have to write a separate class to achieve this for each type I need to have this work for, possibly using interfaces to describe functionality that applies to single instances and arrays of instances.

Is there any Java magic, Design Pattern or Generic jiggery-pokery that can elegantly achieve this?

Further, if not, do any languages facilitate this inherently?

I appreciate it wouldn’t work for all scenarios, but that’s at the discretion of the programmer I suppose.

Many thanks

  • 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-15T14:11:09+00:00Added an answer on June 15, 2026 at 2:11 pm

    You are calling for the Composite Pattern. You can find a componentized generic and reusable implementation in the project PerfectJPattern make sure to checkout the Composite documentation page and the example that matches the example in the GoF book.

    Verbatim copy of the relevant part of the example, say you have IGraphic interface and a few implementations, e.g. Rectangle and Line, then you can do:

    // build the composite
    IComposite<IGraphic> myComposite = new Composite<IGraphic>(IGraphic.class);
    myComposite.add(new Rectangle());                
    myComposite.add(new Line());
    myComposite.add(new Line());
    
    // use the composite, invokes the IGraphic#draw() in the 
    // underlying Rectangle and two Line instances
    myComposite.getComponent().draw();
    

    This is how it would work for your specific case:

    Foo fooInstance1 = new Foo();
    Foo fooInstance2 = new Foo();
    IComposite<Foo> myComposite = new Composite<Foo>(Foo.class);
    myComposite.add(fooInstance1);    
    myComposite.add(fooInstance2);    
    // invokes Method1 on instance1 and instance2 transparently
    myComposite.getComponent().Method1();
    
    // alternatively do
    Foo myCompositeFoo = myComposite.getComponent();
    // pass this myCompositeFoo around and do
    myCompositeFoo.Method1();
    

    Note that the IComposite reusable implementation holds the actual composite and it does not implement/offer the Foo interface, you rather have to get hold of it via the getComponent method. This is a small nuisance and it is needed because there is no other way in Java to create an instance of something (in this case Composite) that implements any arbitrary and statically unknown interface. The best I can do is give you a Composite that underneath builds a true composite component for you and returns your desired interface type Foo. This is implemented using Dynamic proxies, but the implementation is type-safe and fully componentized i.e. you dont have to create any new Composite arrays that implement your interface.

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

Sidebar

Related Questions

Java generics are implemented using type erasure. That means that if I have a
Java design question. I have an object that needs to maintain sets of say
Java: I have shared variable(of type Object) whose value will be changed very frequently
Java has generics and C++ provides a very strong programming model with template s.
Java Newbie here. I have a JFrame that I added to my netbeans project,
Java SE 6 (64 bit only) is now on OS X and that is
Java is nearing version 7. It occurs to me that there must be plenty
Java Docs says that, putIfAbsent is equivalent to if (!map.containsKey(key)) return map.put(key, value); else
Java Exception: http://hastebin.com/yiwecefifi.avrasm I have an object, that I call Category, that my fragment
Java class files inside jars can be easily replaced and modified. For instance, the

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.