|
 |
|
Oracle Java Methods
Oracle Tips by Burleson Consulting
|
All JAVA code begins with the import section. The
import section of a JAVA script or applet is used to tell the
compiler what class packages you are using in the code body. If a
class package is used in the code body and it has not been imported
you will receive an error and your code will not run.
Following the import section, each set of JAVA code
will usually have either a class definition with an init() module or
a main() declaration. Following these major JAVA sections will be
the methods that define the functionality of the script or applet.
Each script or applet can have multiple methods. As in all languages
watch your parenthesis in JAVA they are especially important since
they define the context of your variable definitions. Generally
speaking variables are downwardly referencable but are not upwardly
referencable unless they have been globally defined in the class
definition or at the highest level of the script. An applet must
have an init() method while an application or script will have a
main() method.
In Listing 9 notice the line: public static void
main (String args[]). This tells you that this piece of code is not
an applet that could be run via an HTML (HyperText Markup Language)
call from a web page. This type of script must be run from the java
(note the lower case designation, this is a reference to the
java.exe program). The name of a method includes the method type,
return type and the method name as well as a list of any variables
that must be passed to the method. In the case of public static void
main (String args[]) the method type is public meaning it can be
accessed externally to the class, the return type is static void
which means there is no return type, the method name is main and the
argument passed is a string (character) type called args[] the []
indicates that it is a string array.
Applets include the line: public class <class_name>
extends java.applet.Applet and the: public void init () call.
Applets are run from either a JAVA enabled web browser or from the
appletviewer.exe program. An applet must be wrapped in HTML to be
run using the appletviewer.
TIP:Class Name Usage
If a class is imported then only the class path
following the imported section of the class path needs to be
included in internal references. For example, if the import line for
the Applet class is: import java.applet.* this indicates that
everything after java.applet in the class hierarchy is to be
included. This inclusion means we can now refer to
java.applet.Applet as just Applet in code references.
Overriding of Methods in JAVA
Frequently you will need to add functionality to a
JAVA method, or, fill in a method stub. A good example of a
situation where an override of a method is required is the
paint(Graphics g) method used in graphics work. As it exists, the
paint(Graphics g) method is just a stub, or if you prefer, an null
method. Usually you must add functionality to the paint(Graphics g)
method to make graphics work properly. For example, the simplest
form of this method override would be a call to the method that is
used to draw graphics with a default location specification.
Location specifications for graphics are in pixels and correspond
roughly to the resolution of your monitor driver (for example 1024 x
768 pixels) the location is an (x,y) data point. The simplest
override method for the paint(Graphics g) method would look like
Listing 7.
public void paint (Graphics g)
{
g.drawImage(imagename,0,0,this);
}
Listing 7: Example of Method Overriding
Methods such as paint(), update(), repaint() will
usually be overridden to generate desired behaviors. Other methods,
again, mostly related to graphics generation, such as
getPreferredSize() are usually overridden as required. I suggest
reading up on the method before deciding to override it.
|