Java Reference
In-Depth Information
The AccessDemo class does not compile, and generates seven compiler
errors, all access violations. Study the AccessDemo program carefully and see
if you can spot all seven errors. Figure 7.5 shows the output when an attempt
is made to compile AccessDemo.
package programs;
import electronics.*;
import products.*;
public class AccessDemo
{
public static void main(String [] args)
{
InventoryItem x = new InventoryItem();
InventoryItem y = new InventoryItem(5005678L, “cell phone”);
y.partNumber = -1;
System.out.println(y.getPartNumber());
DVDPlayer z = new DVDPlayer(“Acme”, “2000DV”, 11223344L);
z.setRetailPrice(199.99);
z.retailPrice = 199.99;
DVDPlayer w = new DVDPlayer(“Acme”, “1000DV”);
System.out.println(w.getPartNumber());
System.out.println(w.partNumber);
}
}
AccessDemo is compiled in Figure 7.5 using the -d flag, which is needed
since AccessDemo is in a package (named programs). The dot (.) following -d
represents the current directory. This means that a program's directory will be
created as a subdirectory of whatever directory the AccessDemo.java file is in.
Attempting to access the partNumber field or invoke the getPartNumber()
method accounts for four of the compiler errors in AccessDemo. Neither the
field nor the method is public, so they cannot be accessed from within Access-
Demo.
Figure 7.5
The AccessDemo class generates seven compiler errors.
Search WWH ::




Custom Search