public class BankAccount
{
private double balance;
private int accountNumber;
private static int lastAssignedNumber = 1000;
...
}
If lastAssignedNumber was not static, each instance of BankAccount would have its own value of lastAssignedNumber
public BankAccount() { lastAssignedNumber++; // Updates the static variable accountNumber = lastAssignedNumber; // Sets the instance variable }
public class BankAccount
{
private static int lastAssignedNumber = 1000;
. . .
}
public class BankAccount { . . . public static final double OVERDRAFT_FEE = 5; // Refer to it as BankAccount.OVERDRAFT_FEE }
Harry tells you that he has found a great way to avoid those pesky objects and classes: Put all code into a single class and declare all methods and variables static. Then main can call the other static methods, and all of them can access the static variables. What do you think of Harry's plan?
main
public class RectangleTester { public static double area(Rectangle rect) { double r = rect.getWidth() * rect.getHeight(); return r; } public static void main(String[] args) { Rectangle r = new Rectangle(5, 10, 20, 30); double a = area(r); System.out.println(r); } }
Rectangle r = new Rectangle(5, 10, 20, 30); if (x >= 0) { double r = Math.sqrt(x); // Error - can't declare another variable called r here . . . }
if (x >= 0) { double r = Math.sqrt(x); . . . } // Scope of r ends here else { Rectangle r = new Rectangle(5, 10, 20, 30); // OK - it is legal to declare another r here . . . }
public class Coin { private String name; private double value; // Instance variable . . . public double getExchangeValue(double exchangeRate) { double value; // Local variable with the same name . . . return value; } }
value = this.value * exchangeRate;
public Coin(double value, String name) { this.value = value; this.name = name; }
Consider the following program that uses two variables named r.
public class RectangleTester { public static double area(Rectangle rect) { double r = rect.getWidth() * rect.getHeight(); return r; } public static void main(String[] args) { Rectangle r = new Rectangle(5, 10, 20, 30); double a = area(r); System.out.println(r); } }
r
overlap.this.r
instead
of r
Package | Purpose | Sample Class |
---|---|---|
java.lang | Language support | Math |
java.util | Utilities | Random |
java.io | Input and output | PrintStream |
java.awt | Abstract Windowing Toolkit | Color |
java.applet | Applets | Applet |
java.net | Networking | Socket |
java.sql | Database Access | ResultSet |
javax.swing | Swing user interface | JButton |
omg.w3c.dom | Document Object Model for XML documents | Document |
package packageName;
as the first instruction in the source file containing the classes
package com.horstmann.bigjava; public class Financial { . . . }
java.util.Scanner in = new java.util.Scanner(System.in);
import java.util.Scanner; . . . Scanner in = new Scanner(System.in)
import java.util.*;
java.util.Timer
vs.
javax.swing.Timer
com.horstmann.bigjava
com/horstmann/bigjava/Financial.java
Which of the following are packages?
Harry has found a great way of getting rid of all those pesky
import
statements. Instead of placing import
java.util.Random
at the top of the program, he simply writes
java.util.Random
instead of Random
inside the
program. What do you think?
import
statements to import
packages other than java.lang