views:

235

answers:

4

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?

+2  A: 

Short answer: nope.

However, you can (and should) import classes so as to not use their fully qualified name:

import java.lang.String
// ....
String s = "hello, world.";

If you must define an alias since your class is using multi-level generics or whatnot, you can use this hack - by defining a private class which extends the class you're aliasing (generics included) just for the sake of having an easy-to-use handle:

class MyMap extends HashMap<String, String> {}

MyMap a = new MyMap();
a.put("key", "val");

(adding class aliases was requested before as an enhancement to Java, and is still pending)

Yuval A
@Yuval - that is a bad example. All classes in `java.lang` are implicitly imported on demand.
Stephen C
@Stephen - that's not the point of the question, this is just an example...
Yuval A
@Yuval - I don't want to create a new type. Will be more trouble than benefit.
mykhaylo
@Yuval - huh? I >>KNOW<< it is "just an example". If you use as an example a class that doesn't need to be imported, you may lead newbies to thinking that they need to import everything. Pick a better example; i.e. any class that is not in `java.lang`.
Stephen C
@mykhaylo - I was just suggesting a hack. As I mentioned in my answer, Java gives you no better way of achieving this.
Yuval A
@Yuval - also, if you look again at the bug parade issue, you will see that it is closed as a duplicate of another RFE, and the latter is still open. So it is a bit misleading to state that the enhancement has been denied.
Stephen C
Creating a new class using concrete generic types (like your MyMap example) is useful for clarifying intentions.
Stephen Denne
@Stephen - no dice. If every example I (or anybody else) ever posted on SO was 100%-newbie-safe, I'd (we would) be in a world of pain.
Yuval A
-1 for misleading example and misleading aside about the status of the RFE
Stephen C
@Yuval - Of course, Thanks for Your reply. Any hint is valuable to me. But creating a new type is not an option for me.
mykhaylo
+5  A: 

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));
polygenelubricants
Yes, I know. But sometimes type name(without namespace/package) is to long. Example: HashMap<MyLongTypeName1, MyLongTypeName2> - it is too long for me... Thanks for the answer.
mykhaylo
No aliasing, sorry. Too few advantages, too many disadvantages.
polygenelubricants
You can actually do public static class MyHashMap extends HashMap<MyLongTypeName1, MyLongTypeName2> { }And then do: new MyHashMap() anywhere you need it.
Dean Povey
@Dean: that's not aliasing, that's defining a new class, which OP specifically wants to avoid.
polygenelubricants
A: 

No you can not do like that in java.Here you need to import the packages, if you do not want to import the package then you need to use fully qualified class name.

giri
A: 

I second Yuval's subclass trick. It's not a big deal in performance or semantics.

In C#, Dictionary<string, string> is also a NEW type; each instantiation of a generic type creates a new class at runtime.

irreputable