Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Associate Certification (OCAJP 8)
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Associate Certification (OCAJP 8)
Import on demand class.asterisk
Sergej Smoljanov
Ranch Hand
Posts: 472
10
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.lang.String.*;
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: 3648
16
I like...
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Apart from the import, how are you using the methods of the String class?
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
Sergej Smoljanov
Ranch Hand
Posts: 472
10
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i thisk for import member (static method) of class use:
import static java.lang.String.*
(String is only example, same with Byte and others)
and for non static import i think it used for import type, like
import java.util.ArrayList
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: 2236
63
I like...
posted 9 years ago
2
Number of slices to send:
Optional 'thank-you' note:
Send
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: 11604
178
I like...
posted 9 years ago
2
Number of slices to send:
Optional 'thank-you' note:
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:
package com.ocajp7; public class MyClass { public static class MyStaticInnerClass1{} public class MyInnerClass2 {} }
And this
test
class:
import com.ocajp7.MyClass; // import1 import com.ocajp7.MyClass.*; // import2 public class TestMyClass { public static void main(String[] args) { MyClass clazz = new MyClass(); // line1 MyStaticInnerClass1 clazz1a = new MyStaticInnerClass1(); // line2 MyInnerClass2 clazz2 = clazz.new MyInnerClass2(); // line3 MyClass.MyStaticInnerClass1 clazz1b = new MyClass.MyStaticInnerClass1(); // line4 } }
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.
SCJA
, SCJP (
1.4
|
5.0
|
6
),
SCJD
OCAJP 7
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
import
Java starter
Thread Communication
Performing Java Profiling at Runtime
In demand Java technologies
More...