• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question about when to import or not

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic