Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class or method alias in java

Tags:

java

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?

like image 350
user121196 Avatar asked Feb 05 '10 00:02

user121196


People also ask

What are aliases in Java?

In Java, Alias is used when reference, which is of more than one, is linked to the same object. The issue with aliasing is when a user writes to a particular object, and the owner for the several other references do not expect that object to change.

Can method and class name be same?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.

Can class have two methods same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.


2 Answers

No.

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

like image 184
Thorbjørn Ravn Andersen Avatar answered Sep 22 '22 15:09

Thorbjørn Ravn Andersen


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.

like image 40
Bill K Avatar answered Sep 20 '22 15:09

Bill K