Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias for class name in the method

I have some curly question..
I can define alias for the class at top of my document, Such as

using MyName = Bla.Bla.ClassName

But can I define something like this in the method?

like image 497
Yuriy Avatar asked Sep 22 '10 07:09

Yuriy


People also ask

Is object an alias name for class 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.

What is alias in Java?

An alias is a reference to another object, the same as a synonym.

What is alias method in Rails?

Updated on March 07, 2019. To alias a method or variable name in Ruby is to create a second name for the method or variable. Aliasing can be used either to provide more expressive options to the programmer using the class or to help override methods and change the behavior of the class or object.

Can we use alias in Java?

The aliases() method is a built-in method of the java.


2 Answers

No, you can't. If you mean that you want to define a class alias inside a method, this is not possible.

Alias could be defined only at file or namespace level:

using MyName = Bla.Bla.ClassName;
namespace A{
...
}

or

namespace A{
    using MyName = Bla.Bla.ClassName
...
}

If you instead mean you want to define a "method alias", this also isn't possible: alias are only for types or namespaces.

like image 162
Andrea Parodi Avatar answered Oct 12 '22 13:10

Andrea Parodi


No, using directives have to either be outside any declaration, or within a namespace declaration:

using Foo;
namespace Bar
{
   using Baz;
}

You can't do this within a method. Why not just do it for the whole class though? Why do you only want it to apply within a particular method?

like image 41
Jon Skeet Avatar answered Oct 12 '22 13:10

Jon Skeet