Transcript Document

Lecture 15.1
Static Methods
and Variables
Static Methods
In Java it is possible to declare methods and variables to belong to a class rather
than an object. This is done by declaring them to be static.
the declaration
Static methods are
declared by inserting the
word “static” immediately
after the scope specifier
(public, private or
protected).
public class ArrayStuff {
public static double mean(double[] arr) {
double total = 0.0;
for (int k=0; k!=arr.length; k++) {
total = total + arr[k];
}
return total / arr.length;
}
}
the call
Static methods are called
using the name of their
class in place of
an object reference.
double[] myArray = {1.1, 2.2, 3.3};
...
double average = ArrayStuff.mean(myArray);
15.1.2
Static Methods - Why?
Static methods are useful for methods that are disassociated from all objects,
excepting their parameters.
A good example of the utility of static method is found in the
standard Java class, called Math.
public class Math {
public static double abs(double d) {...}
public static int abs(int k) {...}
public static double cos(double d) {...}
public static double pow(double b, double exp) {...}
public static double random() {...}
public static int round(float f) {...}
public static long round(double d) {...}
public static double sin(double d) {...}
public static double sqrt(double d) {...}
public static double tan(double d) {...}
...
}
15.1.3
Static Method Restrictions
Since a static method belongs to a class, not an object, there are limitations.
The body of a static method cannot reference any non-static (instance) variable.
The body of a static method cannot call any non-static method unless it is
applied to some other instantiated object.
However, ...
The body of a static method can instantiate objects.
Example
(the run.java file)
public class run {
public static void main(String[] args) {
Driver driver = new Driver();
}
}
Note that Java applications are required to initiate execution
from a static void method that is always named main and has
a single String array as its parameter.
15.1.4
Static Variables
Any instance variable can be declared static by including the word “static”
immediately before the type specification
public class StaticStuff {
public static double staticDouble;
public static String staticString;
...
}
What’s Different about a static variable?
1) A static variable can be referenced either using its class name or
a reference to an object of that type.
2) Instantiating a second object of the same type does not increase
the number of static variables.
Example
StaticStuff s1, s2;
s1 = new StaticStuff();
s2 = new StaticStuff();
s1.staticDouble = 3.7;
System.out.println( s1.staticDouble );
System.out.println( s2.staticDouble );
s1.staticString = “abc”;
s2.staticString = “xyz”;
System.out.println( s1.staticString );
System.out.println( s2.staticString );
15.1.5
Static Variables - Why?
Static variables are useful for situations where data needs to be shared across
multiple objects of the same type.
A good example of the utility of static variable is found in the
standard Java class, called Color.
public class Color {
public static final Color black = new Color(0,0,0);
public static final Color blue = new Color(0,0,255);
public static final Color cyan = new Color(0,255,255);
public static final Color darkGray = new Color(64,64,64);
...
}
Since they the Color constants are both static and final, they
can be compared using ==.
Color myColor;
...
if (myColor == Color.green) ...
15.1.6
Java Application
An application is one type of Java Program.
Every application begins executing with a main method.
• main must be static and void.
• main must have a single parameter of type String[].
Example (to start a Driver program)
public class go {
public static void main(String[] args) {
Driver d = new Driver();
}
}
When the go class executes, it instantiates a local object by
calling the Driver constructor method.
Such an application can be executed by the following jdk command
java go
15.1.7
Java Application from BlueJ
Java jar files are a kind of compressed double-clickable executable.
To create a jar file from CS120 programs using BlueJ.
• write a static void main method in the Driver class
• from the BlueJ menu select
Project -> Create Jar File…
Example (the Driver program)
public class Driver {
// all previous code stays here
public static void main(String[] args) {
Driver d = new Driver();
}
}
15.1.8
Java Applet
An applet is the second type of Java Program.
Applets are designed to be delivered to Internet Web browsers which means...
• An Applet has a built-in graphical window.
• An Applet has security restrictions (e.g., it cannot write to a local file).
• Initiating applet execution is different from initiating an application.
Requirements
1) An begins with a class that inherits the Applet or JApplet class.
import java.applet.Applet;
public class Example extends Applet {
...
}
2) An HTML file is needed to invoke the applet.
3) The HTML file must be accessed either by a Web browser or by appletviewer
(a jdk command).
4) Applets have certain methods with predefined purposes. These can be overridden.
These methods include init(), start(), stop(), destroy(). (Parameterless constructor
methods can also be used, but static void main cannot.)
15.1.9
HTML
HTML (HyperText Mark up Language) is a common notation for text files
used on the Internet.
HTML files incorporate the use of tags (like type-setting commands)
< ... >
Example
<html>
<head>
<title> runs the Example applet from the prior slide. </title>
</head>
<body>
<applet code=“Example.class” width=100 height=50>
</applet>
</body>
</html>
program name (This class should
be in the same directory as this
HTML file.)
These two optional parts of the tag
specify the dimensions of the
drawing window used by the applet.
15.1.10
Applets from Driver
The example programs (using the same Driver.java files) from The Object
of Java can be executed as applets in the following way.
1) Include the
following applet in
the program
directory.
import javax.swing.JApplet;
public class AppletStarter extends JApplet {
private Driver driver;
public void start() {
driver = new Driver();
}
}
2) Include a copy of all .class files used by the program in the program folder.
3) Include the
following HTML
file, call it “go.html”
in the program
folder.
<html>
<body>
<applet code=“AppletStarter.class”
width=600 height=500>
</applet>
</body>
</html>
4) Start the applet by opening the HTML file in a Web browser or with the
following jdk command... appletviewer go.html
15.1.11