aspose file tools*
The moose likes Beginning Java and the fly likes Question about when to import or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about when to import or not" Watch "Question about when to import or not" New topic
Author

Question about when to import or not

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Recently i noticed while coding in a servlet that if i use the following statement:

I need the following import


but when i use the following statement :

I do not need the import .. Why is that ?? Isnt getServletConfig() returning a ServletConfig type and the import of ServletConfig is missing ?? Why is that working without an import.

Don’t look where you fall, but where you slipped
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Basic java: if you don't have a reference to the class, you don't need an import.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:Basic java: if you don't have a reference to the class, you don't need an import.

I am a bit confused here ... especially since

is a method in javax.servlet.ServletConfig
and we are not importing that.
So how can that method be available and how does the intellisense display that method??
A little more insight or specific reference material would be appreciated,..
Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you use the name of a type in an abbreviated fashion (i.e. without the package part of the name) then you have to import that name. In your first example you used the name (ServletConfig) without specifying the package, so you need an import.

In all of your other examples you didn't use the names of any types, so that rule doesn't apply. In particular your imaginary rule about having to import methods is just that -- imaginary.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:If you use the name of a type in an abbreviated fashion (i.e. without the package part of the name) then you have to import that name. In your first example you used the name (ServletConfig) without specifying the package, so you need an import.

In all of your other examples you didn't use the names of any types, so that rule doesn't apply. In particular your imaginary rule about having to import methods is just that -- imaginary.


Oh okay thanks for clarifying this... One last question I guess this might be a bit too deep for me but just out of curiousity so when i use something like

where does eclipse look into to generate a list of available methods since the import is missing... ??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That method is called on an object of a known type (in your case the object is "this", which is probably an HttpServlet). So the compiler simply looks at that known type to see what methods it has which matches the given signature.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Adam Zedan wrote:where does eclipse look into to generate a list of available methods since the import is missing... ??

Hi Adam,
eclipse *uses reflection* to identify the list of methods in ServletConfig interface .
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

And in all cases you need the jar containing the 'object' to be on your classpath or in won't work!!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about when to import or not