I am trying to figure out how to alias a class
public class Mappy {
public static Class<Asyncc> Map = Mappy.class;
}
importing the above might look like:
import org.foo.Mappy.Map;
but since Map is a common name, then maybe the user can use the longer name Mappy.
import org.foo.Mappy;
Is this an acceptable way to alias class?
Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name (ยง6.7).
https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.5
There is no way to alias a class or to refer to it by using a name different from the fully qualified name.
Ever had a name clash when two different imports have the same class name?
It happens not that often. #RealSkeptic's comment is a good one.
But I wouldn't use import at all to tell that there are at least two same identifiers expressed differently by providing their full names in the file. It would be annoying to check which static import is used.
Related
I am trying to call a public constructor of a public class located in one package of my project from the main class, located in a class from another package different to the first one, but located in the same project.
I have understood that the public modifier grant you access to methods from any location inside or outside the package, so i just try to create and instance a new object of the public class first mentioned.
F.E: I try something like.... ClassName newObject = new ClassName(); from the main class
Actually, in order to be able of using that (im using netbeans IDE) I need to import the class/method I want to call, even if they have the public modifier.
My question is... is there a way of using these methods without the need of importing them to the main class ?
I am new in this webpage, so sorry if there is anything wrong with the question.
Qualification and visibility have little to do with eachother. Those are different concepts.
Visibility (enforced by access modifiers like public, protected, private and package-private) is about which class may access (i.e. call or use) it.
Qualification is about the compiler asking you: "Okay, you are mentioning a class name, but there could be thousands around with that name. Which one do you mean?"
You could use the fully qualified class Name instead.
a.b.c.MyClass myClass = new a.b.c.MyClass();
Also see
Java: import statement vs fully qualified name?
Java compiler restricts its search for classes inside the package only. In order to make use of any class belonging to a different package, you have to import it explicitly. You can read more about packages here.
Access specifiers are more from restricting the methods from being accessible by outside world. These access specifiers enforce further restrictions on top of what is enforced by packages. You can refer to this link for access specifiers.
I have a class where I use two objects that happen to have the same name.
One is :
com.google.api.services.calendar.model.Event
and the other 3ed party object with the same name, say:
com.some.other.package.Event
Using import for both objects is not a good option because they will mask each other.
Using the very longggggg names all over the code does not look good either.
Creating a "dummy" type just for the sake of changing its name:
public class CEvent extends com.google.api.services.calendar.model.Event {}
does not seem like an elegant solution.
How can I preserve the original object name (Event) yet use a shorter path name ?
In Java it is impossible, the only way is to use full qualified names of classes. However, you can do this in other JVM based languages, such as Scala:
import com.some.other.package.Event => OtherEvent
or Groovy
import com.some.other.package.Event as OtherEvent
Java provides 2 ways:
Use Fully Qualified Class Name for each class, which you don't want to
Use class name for One class and Fully Qualified Class Name for other
Alternate, is to Sub-class the other class, and then you can use the new subclass name. (import sub-class)
You can't shorten class names in Java; you can either import a class name (to use it without qualifiers) or use the fully qualified name. So at least one of the Event classes will have to be referred to by its fully qualified name. (Unless, as you say, you subclass one of them just to save on typing.)
The Java tutorials address this when discussing name ambiguities:
If a member in one package shares its name with a member in another package and both packages are imported, you must refer to each member by its qualified name.
The teacher in our programming lessons is talking about "unqualified names", but I'm wondering what they really are.
I suspect that things such as method names are unqualified, but I'm not certain.
Is there anyone who can explain this to me? I need to know this because I need to explain in what way Java looks an unqualified name up.
A qualified name is one with a full path, e.g.:
java.util.ArrayList list;
An unqualified name has just the last part:
import java.util.*;
ArrayList list;
The term can apply to fields and methods too.
So, if you can import classes, why would you ever need to use a qualified name?
You need it when you're using two classes that, while they're from different packages, share the same name. A classic example is the ridiculously named class from the JDK:
java.sql.Date
which incidentally extends
java.util.Date
It's reasonably common to need references to instances of both class, so you need code that looks like:
public void process(java.util.Date fromDate) {
RowSet rows = <run query with fromDate as parameter>
while (rows.nsxt()) {
java.sql.Date date = rows.getDate(1);
// code that needs date
}
}
If you use two homonymous classes, there's no avoiding qualifying at least one - you can import one, but importing both creates an ambiguity.
A qualified name in Java includes the specific package that the class, interface, enum, or field originated from.
Example: java.util.ArrayList is a fully qualified name.
An unqualified name is simply the class, interface, enum or field without package information.
Example: ArrayList
For example com.yourcompany.domain.Person is the fully qualified class name and Person is the class name or unqualified class name.
Qualified Name: org.springframework.jdbc.core.JdbcTemplate
It has package name org.springframework.jdbc.core then class name JdbcTemplate
Unqualified Name: JdbcTemplate
It is only class name not having package name.
For example : qualified name is whole address of your home and unqualified name is only your home name.
Adding to the conversation a piece that has not been mentioned, yet (and that is not directly asked about but I believe is a helpful adendum to the conversation):
All names in Java require qualification; however, some are so integral to Java's operation that they are assumed - or, defaulted - to be "in the class" you are coding (or imported). Liskov (2000) gives a great example of this: java.lang - the class that contains such objects as String.
You often will see unqualified names in Java. This often has to do with the location of a class or method relative to the class in which you are attempting to access it (same package). Above, other posters have mentioned the concept of packages in Java.
Packages allow you to resolve - or, perhaps, better prevent - naming collisions. Each package in a program can duplicate the class names, etc. of another package. In this case, fully qualified names are used to access the correct class (as is seen in other answers). You can also import a package to avoid using its fully qualified name; however, should two imported classes contain a naming collision, you'll have a bit of problem.
This question already has answers here:
Java: Use import or explicit package / class name?
(3 answers)
Closed 8 years ago.
I came across some java code where the following statement was present.
com.myproject.bar.Foo foo = new com.myproject.bar.Foo();
The class com.myproject.bar.Foo has not been imported into the class but an object of Foo is created in the class where this statement is written.
Why has such an implementation been done? Are there any advantages of using such an implementation over doing an import of the class Foo?
It's instantiation with the fully-qualified name of the class.
com.myproject.bar.Foo foo = new com.myproject.bar.Foo();
This doesn't require to add an import statement, because you've already told the compiler which is the package of the class you want to instantiate.
Sometimes this is used when there are several classes with one and the same simple name.
If you'd like to do this:
Foo foo = new Foo();
then you will have to import the class:
import com.myproject.bar.Foo;
An import statement just makes the type available by its short name without specifying the package. That's all it does. It's not like the class can't be used without an import.
Usually it's clearer to use an import instead, but sometimes that's not possible - you may want to use two classes both called Foo from different packages with the same class, for example. (This is most common when you've got two representations of the same entity - an API representation and a storage representation, for example.)
Without knowing what the real code looks like, we can't tell whether that was the case here, or whether an import would have been fine. If it can work, an import is usually more readable.
If you address the class with fully qualified name such as com.myproject.bar.Foo, you need to specify it every time when you try to access that class.
But in case of import, you dont need to.
That's one of the advantages.
com.myproject.bar.Foo is absolute class name.
Foo is just the Class name(relative) and it can be in any package, so to specify the package, import statements are used.
In cases where you have more than one class with same name, you have to use the absolute class name in your code to distinguish between the duplicate names. Example com.Foo and org.Foo then you use Foo for com.Foo and org.Foo for org.Foo.
There is no special advantages.
But useful in case if two classes have same class name and belongs to different packages then you need to specify the fully qualified package name to differentiate both while using them in a Class.
Consider 2 classes
com.myproject.bar.Foo
com.myproject.bar.innerbar.Foo
Now in some other class you are going to instatiate like
Foo foo = new Foo();
Which Foo to import now?
To avoid that ambiguity, we need to specify the full package name.
I have two imports that I need to use:
import net.robotmedia.billing.model.Transaction;
import com.google.analytics.tracking.android.Transaction;
and I receive the following error:
The import com.google.analytics.tracking.android.Transaction collides with another import statement
But I need to use both of these items. How do I resolve this?
One of the classes must be used by typing its fully qualified name. For example:
net.robotmedia.billing.model.Transaction tx = new net.robotmedia.billing.model.Transaction();
Remember that imports are just used to let you use a class by its simple name rather than using it with its fully qualified name. If you wanted, you could code all your classes without any import, and always use the fully qualified class names. It would just be much less readable and more cumbersome to write. But if two classes with the same simple name are used inside the same class, then you can only import one of them.