class or method alias in java

Java

Java Problem Overview


I have long java class and method names

LONGGGGGGGGGGGGGGGClass.longggggggggggggggggggggggggMethod();

I want to alias it to g.m(); in another class can this be done?

Java Solutions


Solution 1 - Java

No.

Wrap it in a method with a name you like better.

Solution 2 - Java

For one thing, you should rarely be typing the class name. You might have something like this:

import DamnLongPackageNameThatIDontLikeTyping;

class MyCoolClass()
{
    DamnLongClassNameThatIDontLikeTyping dlc=new DamnLongClassNameThatIDontLikeTyping();
    dlc.this();
    dlc.that();
    dlc.tOther();
    dlc.damnLongAnnoyingMethodNameStillHasToBeTypedEveryTime();
}

Okay, so that's not great, but you shouldn't be typing the entire class name very often, just when you first declare it; and the package import makes it so you don't have to type: DamnLongPackageNameThatIDontLikeTyping.DamnLongClassNameThatIDontLikeTyping every time.

Still, that can be annoying to type. Enter the editor. If you aren't using Eclipse, Netbeans or IntelliJ then you really need to stop reading right now and go install it--load up your project. I'll wait....


Seriously. Go get it. The rest of this won't be any fun without it.


Now, the really neat thing is that to get what I typed above, you just do this:

class MyCoolClass()
{
    DLC<ctrl-space>

After typing that, your file will look like this:

import DamnLongPackageNameThatIDontLikeTyping;

class MyCoolClass()
{
    DamnLongClassNameThatIDontLikeTyping<your cursor here>

Note that you didn't type damn long ANYTHING, just DLC It figured out what class you wanted to import, added an import for it and stuck the class in there. (You may have to choose from a list of classes if there is more than one match).

On top of that, once you have an object named dlc instantiated you can type:

dlc.<ctrl-space> and get a list of methods in that class. NEVER AGAIN TYPE A METHOD NAME. If there are a kagillion methods in your class, don't scroll over them, type: dlc.dLAM<ctrl-space> to get dlc.damnLongAnnoyingMethodNameStillHasToBeTypedEveryTime();

Never type a long method name again.

No long method names, no long class names, no long package names. Yet you get extremely readable methods, packages and classes. This is why java programmers tend to use these long names, we also try to remember that we are coding for the next guy and don't want him to have to run all over our code trying to figure out what:

g.m(); refers to -- forcing them to remember that in this class it means GreatClass.motion, but in the next class it means Grey.modifyColor -- that would be really cruel.

Java being statically typed places a LOT of power into the editor. It can do things that you can't even dream of doing with dynamically typed languages, and you should play to the strength of your language to be an effective programmer -- not try to fit each language into some style you learned from using another language.

Note that this works for static methods as well...

DLC<ctrl-space>.dLM<ctrl-space> would be replaced by a call to DamnLongClass.damnLongMethod(), and it would even include the parens for you in 9 keystrokes.

Solution 3 - Java

The Java language provides no aliasing mechanism.

However, you could ease your "pain" somewhat by some combination of the following:

  • For static methods, you can use static imports to avoid having the long class name.

  • You could declare your own convenience class with a short name and short method names, and implement the static methods to delegate to the real methods like:

    public static void shortName(...) { VeryLongClassName.veryLongMethodName(...); }

  • For regular methods, you could implement a Wrapper class, or a subclass with more convenient method names. However, both have downsides from the maintenance and (depending on your JVM) performance perspectives.

  • In Java 8 and later, you could potentially take a method reference, assign it to a named variable, and use that to make your calls.

But lets step back:

  • If the real problem is that you are just fed up with typing long names, a solution is to use a modern IDE that supports completion of names as you type them. See @BillK's answer for example.

  • If the real problem is that you are fed up with the long names taking to much space, a solution is to use a wider screen / longer lines. Most monitors are big enough to display 120 character (or more) wide source code with no eye strain.

  • If neither of the above is the answer, consider just refactoring the offending code to use sensible (i.e. shorter) class and method names. Once again, a modern IDE can handle this kind of refactoring quickly and safely.

On the last point, I would consider that the overly long class names and method names are bad style. IMO, you are justified in taking the time to fix them yourself, or suggesting that they be fixed, especially if they constitute a "public" API for some library or module.

Solution 4 - Java

Actually there is a way to get 1/2 of what you're after.

Looking at your example:

LONGGGGGGGGGGGGGGGClass.longggggggggggggggggggggggggMethod();

It appears that longggggggggggggggggggggggggMethod is static. (If it weren't, you'd be prefixing it with a variable name, which you control the size of.)

You can use Java's static import feature to 'alias' or import the static methods of the LONGGGGGGGGGGGGGGGClass into your own class' namespace. Instead of the above code, you would only have to write this:

longggggggggggggggggggggggggMethod();

Solution 5 - Java

You can use inheritance or encapsulation to wrap the original class.

class g extends LONGCLASS 
{ 
   void a() { super.loooonnng(); }
}

or

class g
{
   private LONGCLASS lc;
   void a() { lc.loooonnng(); }
}

Solution 6 - Java

Not supported in Java.

There is an enhancement ticket (7166917) for adding aliases for imports which would be helpful. The idea is this :

import a.very.lng.pckage.* as shortpckg
import my.pckage.IsAVeryLongClassName as MyShort

Solution 7 - Java

public class Shorten
{
    
    public static final Shorten m = new Shorten();

    public int a(params)
    {
        return some_method_with_long_name(params);
    }

    public void b()
    { 
        // whatever static code you want
    }
}

In your main code then:

import static mypackage.Shorten.m;
...
int res = m.a(params);
m.b();
...

This way you effectively alias any static stuff you want, while avoiding warnings.

Solution 8 - Java

I only ran a simple test but I defined an inner class variable. I'm not an expert nor do I know the consequences of doing this but I obtained positive results.

package a.b;

public class Library {
	public static String str;
}

Now write a class to access the static variables from Library

package a.b;

public class Access {
   public class Short extends Library {}
   Short.str;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser121196View Question on Stackoverflow
Solution 1 - JavaThorbjørn Ravn AndersenView Answer on Stackoverflow
Solution 2 - JavaBill KView Answer on Stackoverflow
Solution 3 - JavaStephen CView Answer on Stackoverflow
Solution 4 - JavaDrew WillsView Answer on Stackoverflow
Solution 5 - JavaEric J.View Answer on Stackoverflow
Solution 6 - JavabvdbView Answer on Stackoverflow
Solution 7 - JavaIvan SkalauhView Answer on Stackoverflow
Solution 8 - JavaSean CountermanView Answer on Stackoverflow