#How can I use two different packages for methods inside one java file?

6 messages · Page 1 of 1 (latest)

thick barn
#

Hello, I have a populator java file where I populate data, but I have two different java files where I will use this populator, but it takes only one specific dataStructure, how can I make so it will be dynamic so i can use it for both java files?


public void populateFromComposition(DataStructure dataStructure, Train input) { ```
And inside my java files i have like populator.populateFromComposition(template, input) for example. But I want to use this dyanmic inside multiply files
warm berryBOT
#

This post has been reserved for your question.

Hey @thick barn! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

unreal scroll
#

One way to make the populator method dynamic so it can be used with multiple Java files and data structures is to use Java Generics. You can modify the signature of your method to accept a generic type parameter instead of a specific data structure, like this:

public <T> void populateFromComposition(T dataStructure, Train input) {
// your implementation here
}

With this change, the method can now accept any data structure type. When you call the method, you specify the actual data structure type as the type argument, like this:

DataStructure template = new DataStructure();
populator.populateFromComposition(template, input);

// Or

AnotherDataStructure anotherTemplate = new AnotherDataStructure();
populator.populateFromComposition(anotherTemplate, input);

thick barn
warm berryBOT