I was fooling around with some Date classes when I came upon the following anomaly.

import java.text.DateFormat;
import java.text.ParseException;

public class Class {
	public static void main(String[] args) {
		DateFormat df = DateFormat.getInstance();
		try {
			System.out.println(df.parse("12/20/10 1:40 AM"));
		} catch(ParseException pe) {
			pe.printStackTrace();
		}
	}
}

This compiles and prints correctly, even though parse(String) returns a Date object. It just seems weird to me that you can call Date's toString() without actually importing Date. Why is this ok?

Because toString() is called using Objects declaration, in that case, and polymorphism causes Date's toString() method to be used.

You do know that imports have no effect on a compiled class, right? They are not even there anymore. They exist solely in the source files and solely for programmer "convenience", that they can type Date rather than having to type java.util.Date

Oh ok.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.