Java Reference
In-Depth Information
System.runFinalization();
This is another of those 'best efforts' deals on the part of the JVM. It will do its very best to run finalize()
for any dead objects that are laying around before return from the runFinalization() method, but
like with a lot of things in this life, there are no guarantees.
Native Methods
It is possible to include a method in a class that is implemented in some other programming language,
such as C or C++, external to the Java Virtual Machine. To specify such a method within a class
definition you use the keyword native in the declaration of the method. For example:
public native long getData(); // Declare a method that is not in Java
Of course the method will have no body in Java since it is defined elsewhere, where all the work is
done, so the declaration ends with a semi-colon. The implementation of a native method will need to
use an interface to the Java environment. The standard API for implementing native methods in C for
example, is called JNI - the J ava N ative I nterface.
The major drawback to using native methods in Java is that your program will no longer be portable.
Security requirements for applets embedded in Web pages require that the code must all be written in
Java - using native methods in an applet is simply not possible. Since the primary reasons for using Java
are the portability of the code and the ability to produce applets, the need for you to add native
methods to your Java programs will be minimal. We will, therefore, not delve any deeper into this topic.
Summary
In this chapter you have learned all the essentials of defining your own classes. You can now create
your own class types to fit the context of the problems you are dealing with. We will build on this in the
next chapter to enable you to add more flexibility to the operations on your class objects by showing
you how to realize polymorphism.
The important points covered in this chapter are:
A class definition specifies the variables and methods that are members of the class.
Each class must be saved in a file with the same name as the class, and with the extension
.java.
Class variables are declared using the keyword static , and one instance of each class
variable is shared amongst all objects of a class.
Each object of a class will have its own instance variables - these are variables declared
without using the keyword static.
Methods that are specified as static can be called even if no class objects exist, but a
static method cannot refer to instance variables.
Search WWH ::




Custom Search