• Post Reply Bookmark Topic Watch Topic
  • New Topic

Import statements  RSS feed

 
Paulius Kukulskis
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

why do we have to import single class like this:

instead of writing something like ? Would this compile the same?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulius Kukulskis wrote:why do we have to import single class like this:

We don't. It's generally regarded as "better practise" though (and it's also the default behaviour for most IDEs) because it's less likely to cause ambiguities - for example:

If you then refer to a 'Date' in your code, which one do you mean? java.util.Date or java.sql.Date?

Another advantage is that anyone looking at your class can see all external class references immediately.

Would this compile the same?

Assuming no ambiguous references: Yes.

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulius Kukulskis wrote: . . . ? Would this compile the same?
Yes, as Winston says.

I have however seen code like this and the writer couldn't understand why Timer was shown as ambiguous and why it failed to compile.I used individual imports in the same exercise and had no such problems.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!