Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How come this 'import' doesn't work?

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When these 2 codes are compiled,
File X1.java

File X2.java

it will result in a compilation error:


X1.java:1: '.' expected
import X2 ;
^


But based on JLS 7.5.1, import can be made using the following synthax:
import TypeName;
where


TypeName must be the canonical name of a class or interface type;


And in section 6.7


The fully qualified name of a top level class or top level interface that is declared in an unnamed package is the simple name of the class or interface.


So in the example above, the canonical name of the top level class X2 is just simply X2. Therefore, importing X2 to X1 should simply be
import X2;
But why is the compiler complaining?
Thanks.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JDK 1.4 release notes:

"The compiler now rejects import statements that import a type from the unnamed namespace. Previous versions of the compiler would accept such import declarations, even though they were arguably not allowed by the language (because the type name appearing in the import clause is not in scope). The specification is being clarified to state clearly that you cannot have a simple name in an import statement, nor can you import from the unnamed namespace.
To summarize, the syntax
import SimpleName;
is no longer legal. Nor is the syntax
import ClassInUnnamedNamespace.Nested;
which would import a nested class from the unnamed namespace. To fix such problems in your code, move all of the classes from the unnamed namespace into a named namespace."
For more info see here
[ February 28, 2004: Message edited by: Rob Ross ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob
[ February 28, 2004: Message edited by: Alton Hernandez ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!