Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Import on demand class.asterisk

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • 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?
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the import, how are you using the methods of the String class?
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • 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)
 
Paweł Baczyński
Bartender
Posts: 1878
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic