Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Generic classes=== {{Main|Generics in Java#Generic class definitions}} Classes can be parameterized by adding a type variable inside angle brackets (<code><</code> and <code>></code>) following the class name. It makes possible the use of this type variable in class members instead of actual types. There can be more than one type variable, in which case they are declared in a comma-separated list. It is possible to limit a type variable to a subtype of some specific class or declare an interface that must be implemented by the type. In this case the type variable is appended by the <code>extends</code> keyword followed by a name of the class or the interface. If the variable is constrained by both class and interface or if there are several interfaces, the class name is written first, followed by interface names with <code>& </code> sign used as the delimiter. <syntaxhighlight lang="java"> /* This class has two type variables, T and V. T must be a subtype of ArrayList and implement Formattable interface */ public class Mapper<T extends ArrayList & Formattable, V> { public void add(T array, V item) { // array has add method because it is an ArrayList subclass array.add(item); } } </syntaxhighlight> When a variable of a parameterized type is declared or an instance is created, its type is written exactly in the same format as in the class header, except the actual type is written in the place of the type variable declaration. <syntaxhighlight lang="java"> /* Mapper is created with CustomList as T and Integer as V. CustomList must be a subclass of ArrayList and implement Formattable */ Mapper<CustomList, Integer> mapper = new Mapper<CustomList, Integer>(); </syntaxhighlight> Since Java SE 7, it is possible to use a diamond (<code><></code>) in place of type arguments, in which case the latter will be inferred. The following code in Java SE 7 is equivalent to the code in the previous example: <syntaxhighlight lang="java"> Mapper<CustomList, Integer> mapper = new Mapper<>(); </syntaxhighlight> When declaring a variable for a parameterized type, it is possible to use wildcards instead of explicit type names. Wildcards are expressed by writing <code>?</code> sign instead of the actual type. It is possible to limit possible types to the subclasses or superclasses of some specific class by writing the <code>extends</code> keyword or the <code>super</code> keyword correspondingly followed by the class name. <syntaxhighlight lang="java"> /* Any Mapper instance with CustomList as the first parameter may be used regardless of the second one.*/ Mapper<CustomList, ?> mapper; mapper = new Mapper<CustomList, Boolean>(); mapper = new Mapper<CustomList, Integer>(); /* Will not accept types that use anything but a subclass of Number as the second parameter */ void addMapper(Mapper<?, ? extends Number> mapper) { } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)