You are on page 1of 4

6.1: What does it mean that a class or member is final?

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;

2: What does it mean that a method or field is "static"?


Static variables and methods are instantiated only once per class. In other words they are
class variables, not instance variables. If you change the value of a static variable in a
particular object, the value of that variable changes for all instances of that class.
Static methods can be referenced with the name of the class rather than the name of a
particular object of the class (though that works too). That's how library methods like
System.out.println() work. out is a static field in the java.lang.System class.

6.3: What does it mean that a method or class is abstract?


An abstract class cannot be instantiated. Only its subclasses can be instantiated. You
indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component {

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.

6.7: Can I cast an int to an Integer? a float to a Float?


No, you cannot promote a base data type like int or float to an object such as an
Integer or a Float. However the proper way to do this isn't very hard. Instead do
int x = 5;
myInteger = new Integer(x);

6.10: Does Java have pointers?


No, no, a thousand times no. Java does not have pointers, no way, no how, the daily email
I get from people who think differently not withstanding.
Java does have references. A reference is an abstract identifier for an object. It is not a
pointer. A reference tags a particular object with a name in the Java virtual machine so
that the programmer may refer to it. How exactly the virtual machine implements
references at the level of machine code is VM-dependent and completely hidden from the
programmer in any case. Most VMs including Sun's use handles, not pointers. A handle is
a pointer to a pointer. At the level of machine code in the CPU a reference is an address in
memory where the address of the object is stored. This way the objects can be moved
around in memory and only the master pointer needs to be updated rather than all
references to the object. This is completely hidden from the Java programmer, though.
Only the implementer of the virtual machine needs to worry about it. Indeed, this is not
the only way references can be implemented. Microsoft's VM actually does use pointers
rather than handles. Other schemes are possible.

6.11: Does Java pass method arguments by value or by reference?


Java passes all arguments by value, not by reference. However this is one of the few
places where the distinction between an object and a reference to an object becomes
important. Object and array variables in Java are really references to the object or array.
This can make it look like an object is passed by reference if you only modify the fields
of the object or array, but do not change the reference itself. For example, consider this
program:
import java.awt.Point;
class changePoint {
public static void main(String args[]) {
Point p1 = new Point(0, 0);
changePoint(p1);
System.out.println(p1);
}
static void changePoint(Point p) {

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.

You might also like