Forums Register Login

Import on demand class.asterisk

+Pie Number of slices to send: Send

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?
+Pie Number of slices to send: Send
Apart from the import, how are you using the methods of the String class?
+Pie Number of slices to send: Send
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)
2
+Pie Number of slices to send: Send
Well, importing java.lang.String.* is allowed because String (well, any class) can have public static nested classes declared inside.
2
+Pie Number of slices to send: Send
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.
    I like tacos! And this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 529 times.
    Similar Threads
    import
    Java starter
    Thread Communication
    Performing Java Profiling at Runtime
    In demand Java technologies
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 15, 2024 21:54:54.