Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"using" directive in Java

When the type name is too long, in C# i can create alias like this:

using Dict = System.Collections.Generic.Dictionary<string, string>;

And I can use it like this:

Dict d = new Dict();
d.Add("key", "value");

Can I create an alias similar to this in Java?

like image 945
Michał Ziober Avatar asked Mar 10 '10 00:03

Michał Ziober


1 Answers

You can't create an alias, but you can import packages (JLS 7.5 Import Declarations) so that you don't have to fully qualify class names in that package.

import java.util.*;
import java.lang.reflect.Field;

....

Set<Field> s = ... // Set is in java.util

You can also do a static import (guide), though this practice should be limited.

import static java.util.Arrays.asList;

...

System.out.println(asList(1, 2, 3));
like image 107
polygenelubricants Avatar answered Sep 20 '22 14:09

polygenelubricants