• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compile time inport & run time import  RSS feed

 
V Senthil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A class "ClassA" has unwanted import statement. As per my understanding, this import statement will not be loaded into Run Time Environment until it is used. So the unwanted import statements in "ClassA" will not be loaded.
If this is the case, suppose I am giving invalid class name in import statement and not using it in my program. Moreover this will not be used by JVM, Why this class gives compile time error?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You seem to have missed reading our policy on display names when you signed up. Your display name must be a real (sounding) first and last name -- no nonalphabetic characters, joke names or "handles" are allowed. You can fix your display name here. Thanks for your attention.

Now, as to your question: there's no such thing as a "run time import." An import directive is an instruction to the compiler regarding about the name which will be used for a class or classes in that source file -- nothing more and nothing less. If you tell the compiler you want to refer to a class foo.bar.X, and the compiler can't find "foo.bar.X", then it only makes sense that the compiler should report an error. If the class X isn't used, then you can simply remove that import to fix the error. If it is used, then you need to make the class available to the compiler so the compilation can proceed.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

The import statement helps the compiler find other classes. If your import tells it you'd like to use another class. If the compiler can't find the class, it considers the import statement an error.

Once it has found the other class at compile time it generates fully qualified class names where those classes are used. If you never use the classes no such names are generated, no error. It generates nothing for the import statement; that was just a hint for compile time.

Eclipse (and probably other editors) will flag unused imports as warnings or errors. I use the Eclipse option to clean up imports often. It just keeps the code cleaner and more honestly self-documenting.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words, the runtime environment doesn't know anything about imports - the java byte code always only contains fully qualified class names.
 
V Senthil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!