Chapter 10
Using Classes
10.1 Getting Access
- The Java distributions is provided with a programmed set of classes, referred to as fundamental classes.
- The programmers may add classes of their own.
- To use a specific feature of a class, we need to specify the name of the feature, the class owning the feature, and the
package where the class belongs.
class prg{
public static void main(String[] args){
java.lang.System.out.println( "Hello world!" );
} }
- A class can be made available with the command
import <package name>.<class name>
In such a case, if no ambiguity arises, the name of the package is not required in the referenced feature. The same
holds for the class name.
import java.lang.System;
class prg{
public static void main(String[] args){
System.out.println( "Hello world!" );
} }
- The following variant of the ‘import’ command loads all the classes from the specified package.
import <package name>.*
import java.lang.*;
class prg{
public static void main(String[] args){
System.out.println( "Hello world!" );
} }
- The classes of ‘java.lang’ are automatically loaded, so there is no need to request them.
class prg{
public static void main(String[] args){
System.out.println( "Hello world!" );
} }
10.2 Constructors
- The values of the primitive data types can be expressed in programs through literals.
- The values of the class data types can be expressed in programs using constructors, through instructions of the
following form.
new <constructor> ( <arguments> )
- The values available for a class data type are said to be objects, and they are cosidered to be instances of the
class.
class example{
public static void main(String[] args){
System.out.println( new java.util.Date() );
System.out.println( new java.util.Date( 123456789L ) );
} }
- A variable of a primitive data type holds a value of the given type.
- A variable of a class data type holds a reference, or a pointer, to an instance of the given data
type.
class example{
public static void main(String[] args){
java.util.Date a, b, c = new java.util.Date();
a = new java.util.Date();
b = a;
System.out.println( b );
} }
10.3 Using Instance Methods
import java.util.Date;
class prg{
public static void main( String [] args ){
Date a, b;
int c;
a = new Date ( 1L );
b = new Date ( 2L );
c = a.compareTo( b );
} }
10.4 Using Class Methods
// java.lang.Math: static double abs(double a)
class prg{
public static void main( String [] args ){
double a, b;
a = -1.2;
b = Math.abs( a );
} }
10.5 Accessing Fields
- An instance field holds infomation about the specific instance.
- A reference to an instance field should be quatified to the instance.
<instance>.<field>
- A class field holds infomation regarding the class as a group.
- A reference to a calss field should be quatified to the class name.
<class name>.<field>
Exercises
10.6 Assignment #11: Referencing Constructors and Methods
Due: We, Apr 30, midnight
Consider the following incomplete program.
class lab11{
public static void main(String[] args){
Integer in;
in = new Integer ( );
System.out.println( in );
Long ;
ln = new Long ( );
System.out.println( ln );
Boolean bo;
bo = new ( false );
System.out.println( bo );
Double db;
= new Double ( );
println( );
java.util.Date da;
da = new Date ( );
System.out.println( da );
String st;
st = new String("A string. ");
st = concat ( st );
System.out.println( st );
} }
Add the missing code to the program, so that it will produce the following output.
1234
123456789012345
false
1.2E-33
Sat Jan 01 00:00:00 EST 2000
A string. A string.
Notes:
- You are not allowed to remove any code from the given program
- Assume lab ‘lab11’ for the submit program, and file name ‘lab11.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A