• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about when to import or not

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic java: if you don't have a reference to the class, you don't need an import.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in all cases you need the jar containing the 'object' to be on your classpath or in won't work!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic