Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved 15.1.2 Static Methods In Java it is possible to declare.

Similar presentations


Presentation on theme: "Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved 15.1.2 Static Methods In Java it is possible to declare."— Presentation transcript:

1 Lecture 15.1 Static Methods and Variables

2 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.2 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). the call Static methods are called using the name of their class in place of an object reference. 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; } 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; } double[] myArray = {1.1, 2.2, 3.3};... double average = ArrayStuff.mean(myArray); double[] myArray = {1.1, 2.2, 3.3};... double average = ArrayStuff.mean(myArray);

3 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.3 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) {...}... } 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) {...}... }

4 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.4 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. Example (the run.java file) The body of a static method cannot call any non-static method unless it is applied to some other instantiated object. The body of a static method can instantiate objects. However,... public class run { public static void main(String[] args) { Driver driver = new Driver(); } 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.

5 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.5 Static Variables Any instance variable can be declared static by including the word “static” immediately before the type specification What’s Different about a static variable? 1) A static variable can be referenced either using its class name or an name object. public class StaticStuff { public static double staticDouble; public static String staticString;... } public class StaticStuff { public static double staticDouble; public static String staticString;... } 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 ); 2) Instantiating a second object of the same type does not increase the number of static variables. Example

6 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.6 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);... } 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)...

7 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.7 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

8 © 2006 Pearson Addison-Wesley. All rights reserved 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). Requirements import java.applet.Applet; public class Example extends Applet {... } Initiating applet execution is different from initiating an application. 1) An begins with a class that inherits the Applet class. 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.) 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).

9 © 2006 Pearson Addison-Wesley. All rights reserved 15.1.9 HTML HTML (HyperText Mark up Language) is a common notation for text files used on the Internet. Example runs the Example applet from the prior slide. HTML files incorporate the use of tags (like type-setting commands) 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.

10 © 2006 Pearson Addison-Wesley. All rights reserved 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. import java.applet.*; public class AppletStarter extends Applet { private Driver driver; public void start() { driver = new Driver(); } 1) Include the following applet in the program directory. 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. 4) Start the applet by opening the HTML file in a Web browser or with the following jdk command... appletviewer go.html


Download ppt "Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved 15.1.2 Static Methods In Java it is possible to declare."

Similar presentations


Ads by Google