274. Java auto imports all the classes in java.lang package

All the Classes created in Java imports the classes in java.lang package by default. Hence we wont  get the compiler errors while using the Classes of java.lang package. For example - System Class is a class of java.lang package. But we don't get any compiler error to import the java.lang.System; when ever we use System.out.println(" "); statements in our Java programs.

Lets implement and see on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project 'Project 53' as shown below -

2. Create a Class 'JavaLangPackage' with main( ) method as shown below -

3. Use System Class of java.lang package by using the System.out.println(" "); statement and observe that no compiler error is displayed to import the java.lang package as shown below -

System Class in System.out.println("Hi"); statement in the above screen has not given any compiler error to import java.lang.System; . Hence all the Java Classes auto imports all the Classes in java.lang package.

Where as when we use the File Class of package in our java programs,  the compiler error to import the; will be displayed.

Lets implement on Eclipse IDE -

1. In the above program, write the File Class object creation statement as shown below and observe than compiler error with an option to import the; is displayed as shown below -

2. Select the 'Import File (' option from the above error message and observe that; is imported in our program and the error got resolved as shown below -

Hence we need to manually import the predefined Classes of Java in other packages like etc. But there is no need to manually import the predefined Classes of Java in java.lang packages, as the Java automatically imports all the Classes of java.lang package in our programs.

Please comment below to feedback or ask questions.

Introduction to Selenium WebDriver  will be explained in the next post.


Prasu said...

Hi Arun, thanks for your information on Selenium, I am learning lot of things. when you post the next chapter.

saket lakhotia said...

Hello Arun,

How many more post we are going to have on Java?

Arun Motoori said...

@ Prasue and Saket Lakhotia - I have covered the main concepts of Java. I can say that I have covered 95% of Java Posts. Remaining 5% of pending posts wont stop you from learning Selenium WebDriver. So instead of waiting for the remaining Java posts, you can get started with Selenium WebDriver posts explained at (If in case anyone is using the older version of Selenium in their projects i.e. Selenium RC, you can learn the Selenium RC posts at )

Sowmya Bommakanti said...

Hi Arun,

Wonderful blog and I must thank you for explaining every topic very clearly with suitable examples. Need not to say that your efforts did not go vain.

I could not see any posts 'aagh.164after Using @Before Class and @AfterClass TestNG Annotations'. Did you post anything after it? How can I navigate to?

Arun Motoori said...

@Sowmya - Check the Contents page where you have all the contents.

Pooja Ojha said...

Hi Arun.
The post comment option is not enabled for the Seleniumtwo blog. Can you please do the needful, have few queries on post number 27, will comment on the same to get it clarify from you. Thank you for your time.