Log in from a computer to take this course

You'll need to log in from a computer to start Learn Intermediate Java. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

As of now, our generic classes, interfaces, and methods have all taken a single parameter type but what if our program needed to specify two or more parameter types? Java generics allow us to do that as well, let’s look at an example:

public class Box<T, S> { private T item1; private S item2; // Constructors, getters, and setters } Box<String, Integer> wordAndIntegerBox = new Box<>("Hello", 5);

In the example above we’ve created a generic class Box with two type parameters, T and S by providing a comma-separated list of type parameters in the diamond operator. We also instantiate a Box reference named wordAndIntegerBox by providing the necessary type arguments in a comma-separated list, <String, Integer>.

This can also be similarly done for interfaces and methods. Let’s look at an example for a method:

public class Util { public static <T, S> boolean areNumbers(T item1, S item2) { return item1 instanceof Number && item2 istanceof Number; } } boolean areNums = Util.areNumbers("Hello", 34); // false

In the example above, we’ve created a static areNumber() method that has two type parameters T and S. Note that a comma-separated list of type parameters, <T, S>, must be specified in the method signature prior to the return type. A cool thing about the example is if it weren’t for Java’s type interference, the above method would have to be called like:

Boolean areNums = Util.<String, Integer>areNumbers("Hello", 34); // false

In the example above, we explicitly specified the type arguments <String, Integer> before the method name. Type inferences will infer these types from the arguments passed in, "Hello" and 34, making the explicit arguments unnecessary.

Let’s practice creating a multiple-type parameter class.

Instructions

1.

Currently Main.java won’t compile because Container only has a single type parameter T, let’s fix the errors!

In Container.java, add a second type parameter, S, to the Container class.

Note: It’s okay to see an error stating main() is missing in Container.

2.

We’ve made Container have multiple type parameters but now we need a field and constructor so we can create references to Container.

In the class Container, define a private field named item2 of type S. Let’s also modify the constructor to have a second parameter of type S named item2 and initialize the field item2 with the parameter item2.

Note: It’s okay to see an error stating main() is missing in Container.

3.

Let’s finalize Container by creating a getter for the new item2 field.

In the class Container, define a method named getItem2() with a return type of S that returns the field item2.

Note: It’s okay to see an error stating main() is missing in Container.

4.

Great Job amending Container to meet our new needs.

Let’s run main() in Main.java.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?