6.1: What Does It Mean That A Class or Member Is ?: Class Variables, Not Instance Variables. If You Change The Value of A
6.1: What Does It Mean That A Class or Member Is ?: Class Variables, Not Instance Variables. If You Change The Value of A
A final class can no longer be subclassed. Mostly this is done for security reasons with
basic classes like String and Integer. It also allows the compiler to make some
optimizations, and makes thread safety a little easier to achieve.
Methods may be declared final as well. This means they may not be overridden in a
subclass.
Fields can be declared final, too. However, this has a completely different meaning. A
final field cannot be changed after it's initialized, and it must include an initializer
statement where it's declared. For example,
public final double c = 2.998;
It's also possible to make a static field final to get the effect of C++'s const statement
or some uses of C's #define, e.g.
public static final double c = 2.998;
Abstract classes may contain abstract methods. A method declared abstract is not
actually implemented in the current class. It exists only to be overridden in subclasses. It
has no body. For example,
public abstract float price();
Abstract methods may only be included in abstract classes. However, an abstract class is
not required to have any abstract methods, though most of them do.
Each subclass of an abstract class must override the abstract methods of its superclasses
or itself be declared abstract.
p.x = 38;
p.y = 97;
}
}
It prints:
java.awt.Point[x=38,y=97]
Therefore the point has been changed. However the reference, which is what was really
passed, has not been changed. To see that consider the following program.
import java.awt.Point;
class dontChangePoint {
public static void main(String args[]) {
Point p1 = new Point(0, 0);
dontChangePoint(p1);
System.out.println(p1);
}
static void dontChangePoint(Point p) {
p = new Point(38, 97);
}
}
It prints:
java.awt.Point[x=0,y=0]
What happened in this example was that a copy of the reference p1 was passed to the
dontChangePoint() method. A new Point object was then assigned to that copy.
However this did not change the old reference in the main method. In the previous
example the reference p in the changePoint() method and p1 in the main() method
both referred to the same object. In this example p and p1 refer to different objects after
the new Point is assigned to p.
How do I run the garbage collector to free memory and delete unused
objects?
Answer
First, you should be aware that the garbage collector works automatically, whether you
request it to or not. Secondly, you cannot force the garbage collector to run.
That said, you can request the garbage collector to perform work, by invoking the
System.gc() method. Once working, you may experience a short pause while the garbage
collector does its work. That means you should do it at an appropriate time, such as
before a big task or when the GUI is idle and waiting for input.