Java Namespace - two classes with the same name in different packages

I am from Objective-C, where we have no packages and namespacing.

Android has android.text.format.DateFormat, which has static methods that return instances of java.text.DateFormat ( getLongDateFormat() and getMediumDateFormat() ).

  • Are these methods called "static methods" or "class methods" or both interchangeably?

  • After looking at the Android documentation , how can I assume that the methods android.text.format.DateFormat return an instance of java.text.DateFormat and not an instance of android.text.format.DateFormat (returning an instance of the latter is what I originally expected)?

  • How to import the necessary packages to be able to use both of these classes in my source?

  • Is it possible to write implementation code in this way:

 DateFormat df = DateFormat.getLongDateFormat(this.getActivity()); mLabel.setText(df.format(mEvent.getDate()); 

Another way I would write would be to use the full package names, but this seems unnecessary:

 java.text.DateFormat df = android.text.format.DateFormat.getLongDateFormat(this.getActivity()); mLabel.setText(df.format(mEvent.getDate()); 
+7
java namespaces packages
source share
2 answers

Not sure why this is underestimated, this is a useful discussion.

1) I always heard what they were called "static methods".

2) The only way to see this is to follow the links. In this case, the documentation is definitely misleading.

3/4). The usual way to do this in java is not to import one of the classes and fully qualify its class name. Therefore, if you decided to import java.text.DateFormat , and not the Android version, you would do something like DateFormat df = android.text.format .DateFormat.getLongDateFormat(this.getActivity());

+5
source share
  • From JLS :

    A method declared static is called a class method.

    I would say that I hear a "static method" that is used more often than a "class method", but both of them are used and should be understood by competent Java developers.

  • The only option is to link to the return values. This is an example of an extremely poor API architecture, with a name conflict, and android.text.format.DateFormat should have been called something like DateFormatFactory . It looks like this class may have been intended for the same purpose as the java.text class, and API compatibility has left it stuck. See java.sql.Date for a similar story.

  • Using import is just a convenience, allowing you to use the simple class name in your code. It is always useful to use a fully qualified class name, and the compiler translates the imported class names into fully qualified ones. You cannot import multiple classes with the same name, because then there is no way to distinguish them.

  • I suggest importing a class from java.text for two reasons: you are likely to use it more often, and it is a more β€œstandard” class. When you are faced with the choice of qualifying one of two classes with the same simple name, use a simple name for the one that developers usually assume it refers to.

+2
source share

All Articles