Download Static Methods

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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);
© 2006 Pearson Addison-Wesley. All rights reserved
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) {...}
...
}
© 2006 Pearson Addison-Wesley. All rights reserved
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.
© 2006 Pearson Addison-Wesley. All rights reserved
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
an name object.
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 );
© 2006 Pearson Addison-Wesley. All rights
reserved
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) ...
© 2006 Pearson Addison-Wesley. All rights reserved
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
© 2006 Pearson Addison-Wesley. All rights reserved
15.1.7
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 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.)
© 2006 Pearson Addison-Wesley. All rights reserved
15.1.8
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>
</applet>
</body>
</html>
program name (This class should
be in the same directory as this
HTML file.)
© 2006 Pearson Addison-Wesley. All rights reserved
These two optional parts of the tag
specify the dimensions of the
drawing window used by the applet.
15.1.9
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 java.applet.*;
public class AppletStarter extends Applet {
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
© 2006 Pearson Addison-Wesley. All rights reserved
15.1.10