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.
Related
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.
What happens if we use a Java key word as our class name. In my case I created a class "Process". But I can not instantiate it. Is there a way to do it?
You cannot use Java keywords as identifiers in a Java program (to name a class, or a variable for example). However, you did not use a Java keyword, rather you named your class Process just like the java.lang.Process class. Since the java.lang.* package is imported by default in any java class, that is why you cannot instantiate it.
If you really want to instantiate your own Process class just refer to it with its fully qualified name, like: new my.package.Process(....).
But I advise you to change the name, it will give your code more clarity.
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.
Considering everything is object oriented etc, so names have to describe the object and what it is, I have an abstract class that sub classes inherit from. These are all objects on the screen (it's a game), i.e, player, and a weight (trapezoid weight). I have it currently named Character but it doesn't seem fitting as the weight is not a Character itself, it only inherits properties from Character.
Could I call this class "Object" without it breaking conventions? - could someone come up with a more appropriate name?
Technically, you could - but it's a very, very bad idea, so don't.
Longer explanation: The Object class already in Java is java.lang.Object - so there's no technical reason why you could create another Object class in another package, just as you could create another String class in another package. (Actually, technically speaking you could even create your own java.lang.Object, but I'm not even going to go there!)
However:
Could I call this class "Object" without it breaking conventions?
Without breaking convention? Not in the slightest. You should never duplicate such commonly used class names elsewhere, especially those in java.lang. It would be considered incredibly bad code design.
In terms of a better name, Actor or Sprite may be two good alternatives.
Java's Object class is part of the java.lang package which is automatically imported for every class file. If you name your class Object and forget to explicitly import it in other classes, you will have issues, thinking you're using com.custom.Object (your class), but actually using java.lang.Object, the JDK's.
Use a more descriptive name, ApplicationObject.
Yes you can. The class beside the name has the path that is package.
package org.stackoverflow
public class Object {
}
By default java.lang is prohibited package name so you can not do declare
package java.lang
public class Object {
}
The class names does not have to be unique in scope of whole world. Using the class path you are able to override the JVM definition of class.
I'm writing a very basic app, extending the Swing JFrame. What are the differences between making an explicit reference:
public class LineTest extends javax.swing.JFrame {
...
or importing the class beforehand:
import javax.swing.JFrame;
public class LineTest extends JFrame {
...
When (and why) would you use one technique over the other?
There is no real difference; the generated byte code will be exactly the same. An import statement is really just a way to tell the compiler "when I write JFrame, I actually mean javax.swing.JFrame".
You might want to use fully-qualified package names if you have for example two classes with the same name in different packages. One common example from the standard library are the classes java.util.Date and java.sql.Date.
The only difference is in the source code. Using the fully qualified name leads to less readable code, so everyone uses imports pretty much exclusively. The only place where I've ever seen the fully qualified names used consistently is in generated code.
The only reason to use the fully qualified name in regular code is when you have to use two classes with the same simple name (e.g. java.util.List and java.awt.List) - in that case there is no alternative.
For the compiler it doesn't make any difference. The disadvantage of using the full qualified name is that you would have to write it each time you are using the class. I only use the full qualified name if I have two classes with the same name in different packages. This way you can differ between those two classes