6 Lecture Six Encapsulation Access Modifiers and Packagespptx 2
6 Lecture Six Encapsulation Access Modifiers and Packagespptx 2
• Example:
• If you find a class you want to use, for example, the Scanner
class, which is used to get user input, write the following code:
Built-in Packages…
• In the previous example, java.util is a package, while
Scanner is a class of the java.util package.
• To use the Scanner class, create an object of the class
and use any of the available methods found in the
Scanner class documentation.
• In our example, we will use the nextLine() method,
which is used to read a complete line:
• [Look the example in the next slide]
Built-in Packages…
• Using the Scanner class to get user input:
Built-in Packages…
• Import a Package
• There are many packages to choose from. In the previous
example, we used the Scanner class from the java.util
package. This package also contains date and time facilities,
random-number generator and other utility classes.
• To import a whole package, end the sentence with an asterisk
sign (*).
• The following example will import ALL the classes in the
java.util package:
User-defined Packages
• To create your own package, you need to understand that
Java uses a file system directory to store them. Just like
folders on your computer:
• Example: