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

What is difference between import java.lang.System.*; and import java.lang.System; ?

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we replace line1 with import static java.lang.System.*; the code also works properly because we know that import static java.lang.System.*; import all of static members of class. But we remove static from this the statement does still compile but line2 doesn't compile.
Which things are imported when we use import java.lang.System.*; ? And What is difference between import java.lang.System.*; and import java.lang.System; ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Which things are imported when we use import java.lang.System.*; ?

All nested classes which are defined in class java.lang.System.

Mushfiq Mammadov wrote:And What is difference between import java.lang.System.*; and import java.lang.System; ?

import java.lang.System; = the import statement of the java.lang.System class (all classes from the java.lang package are imported by default).
import java.lang.System.*; = as I said before, all nested classes which are defined in class java.lang.System.

So because we can't change the java.lang.System class, let's create another class to illustrate the aboveThe Developer class is a nested (or inner) class of Java. Now let's see how we can use them.If you use the import statement of the outer class (cfr. import java.lang.System;), you'll need OuterClass.InnerClass as the type of a reference variable for the inner class.
Now by using the import statement of the outer class followed by a star (cfr. import java.lang.System.*;), you can use InnerClass as the type of a reference variable for the inner class. Important note: because the import statement only imports the nested classes of the outer class, not the outer class itself; so you need a seperate import statement for the outer class itself, if you want to use it. The following code won't compile!To make this code compile successfully, you'll need an additional import statement for the Java class: import com.ocajp7.Java;

Hope it helps!
Kind regards,
Roel

(Disclaimer: nested/inner classes are not on the OCA exams, it's on the OCP exams. That's why I didn't instantiate them and just used null)
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean this?
import static java.lang.System.*;
That imports all static members of the System class so you can pretend to the compiler that something like arraycopy is a (??private??) static member of your current class. You can then say arraycopy(...) rather than System.arraycopy(...).

Since System is an uninstantiable class, all its public members are static.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mushfiq Mammadov wrote:Which things are imported when we use import java.lang.System.*; ?

All nested classes which are defined in class java.lang.System.

Mushfiq Mammadov wrote:And What is difference between import java.lang.System.*; and import java.lang.System; ?

import java.lang.System; = the import statement of the java.lang.System class (all classes from the java.lang package are imported by default).
import java.lang.System.*; = as I said before, all nested classes which are defined in class java.lang.System.

So because we can't change the java.lang.System class, let's create another class to illustrate the aboveThe Developer class is a nested (or inner) class of Java. Now let's see how we can use them.If you use the import statement of the outer class (cfr. import java.lang.System;), you'll need OuterClass.InnerClass as the type of a reference variable for the inner class.
Now by using the import statement of the outer class followed by a star (cfr. import java.lang.System.*;), you can use InnerClass as the type of a reference variable for the inner class. Important note: because the import statement only imports the nested classes of the outer class, not the outer class itself; so you need a seperate import statement for the outer class itself, if you want to use it. The following code won't compile!To make this code compile successfully, you'll need an additional import statement for the Java class: import com.ocajp7.Java;

Hope it helps!
Kind regards,
Roel

(Disclaimer: nested/inner classes are not on the OCA exams, it's on the OCP exams. That's why I didn't instantiate them and just used null)

Thanks for your elaborate explanation, Roel, everything is clear
P.S. Sorry, what is "cfr." ?

Campbell Ritchie wrote:Do you mean this?
import static java.lang.System.*;
That imports all static members of the System class so you can pretend to the compiler that something like arraycopy is a (??private??) static member of your current class. You can then say arraycopy(...) rather than System.arraycopy(...).

Since System is an uninstantiable class, all its public members are static.

Thanks for your reply, Campbell, Roel have already explained everything detailed.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:P.S. Sorry, what is "cfr." ?

My mistake! In Dutch we use cfr. as the abbreviation, but in English it seems to be cf.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic