How to use the Import Statement

The following is a simple Java application program showing where in a Java program import statements, the method main, and statements such as named constants, declarations, assignment statements, and input and output statements typically appear. I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
  1. import java.util.*;                                                                            
  2.    
  3. public class ImportJava                                                                                
  4. {                                                                                                    
  5.         static final int NUMBER = 12;                                                                          
  6.         static Scanner console = new Scanner(System.in);  
  7.         public static void main(String[] args)                                                              
  8. {                                                                                                                                            
  9.         int firstNum;                                                                                      
  10.         int secondNum;                                                             
  11.  
  12.         firstNum = 18;                                                                             
  13.  
  14.         System.out.println("firstNum = " + firstNum);                                      
  15.         System.out.println("Enter an integer; ");                                  
  16.         secondNum = console.nextInt();                                                   
  17.         System.out.println();                                                                            
  18.         System.out.println("secondNum = " + secondNum);                                
  19.  
  20.         firstNum = firstNum + NUMBER + 2 * secondNum;                                    
  21.  
  22.         System.out.println("The new value of " + "firstNum = " + firstNum);    
  23. }                                                                                
  24. }                                                                                                                                        
The preceding program works as follows: The statement import java.util.*; import the class Scanner. The statement public class ImportJava names the class containing statements of the program as ImportJava. The left brace { marks the beginning of the class ImportJava. The statement static final int NUMBER = 12 declares the named constant NUMBER and sets its value to 12. The statement static Scanner console = new Scanner(System.in) declares and initializes the object console to input the data from the keyboard. The statement public static void main(String[] args) contains the heading of the method main, and the left brace { marks the beginning of the method main. The statements int firstNum and int secondNum; declare he variables firstNum and secondNum. The statement firstNum = 18; sets the value of firstNum to 18, and the statement System.out.println("firstNum = " + firstNum); outputs the value of firstNum. Next, the statement System.out.println("Enter an integer; "); prompts the user to enter an integer. The statement secondNum = console.nextInt(); reads and stores he integer into the variable secondNum, which is 15 in the sample run. The statement System.out.println(); positions the insertion point on the screen at the beginning of the next line. The statement System.out.println("secondNum = " + secondNum); outputs the value of secondNum. The statement firstNum = firstNum + NUMBER + 2 * secondNum; evaluates the expression:
firstNum + NUMBER + 2 * secondNum
and assigns the value of this expression to the variable firstNum, which is 60 in the sample run. The statement System.out.println("The new value of " + "firstNum = " + firstNum); outputs the new value of firstNum. The right brace } marks the end of the method main, and the right brace } marks the end of the class ImportJava. To execute this program, click run. Sample run:
firstNum = 18
Enter an integer:
12

secondNum=12

The new value of firstNum = 54

Add new comment