• Post Reply Bookmark Topic Watch Topic
  • New Topic

Import on demand class.asterisk  RSS feed

 
Sergej Smoljanov
Ranch Hand
Posts: 472
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: 3639
16
Firefox Browser Java Mac OS X
  • 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: 472
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: 2006
42
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: 11096
173
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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!