• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Import on demand class.asterisk

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

there is no compilation error. (i use javac/java and NetBeans - same result all ok)
I expect String is not package, why import on demand allowed?

what rule for this, and what purpose of this behaviour?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the import, how are you using the methods of the String class?
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thisk for import member (static method) of class use:

(String is only example, same with Byte and others)
and for non static import i think it used for import type, like

and if in path there is no right package - had compilation error.
Is this behaviour made for inner classes/interface? (that may be or not in class)
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, importing java.lang.String.* is allowed because String (well, any class) can have public static nested classes declared inside.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any class (even interfaces) can have (static) nested classes. That's why that import-statement is allowed. This is illustrated in a small code example.

Assume this class:


And this test class:


Some observations about this code:
  • Without import1, line1 and line4 won't compile.
  • Without import2, line2 and line3 won't compile (but line4 will compile).


  • Disclaimer: inner classes are not on the OCAJP7 exam.
    reply
      Bookmark Topic Watch Topic
    • New Topic