Lecture 15.1 Static Methods and Variables. 15.1.2 Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
1 CS100J 12 April 2007 Applications and Applets Read Chapter 16 of the text I never let my schooling interfere with my education. Mark Twain Learning makes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets Java API.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Internet Software Development Applets Paul J Krause.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Utilities (Part 2) Implementing static features 1.
1 CS100J 2 December 2008 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
1 CS Nov 2011 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from a real-life.
Chapter 6 Applets and HTML  Overview  HTML tags for Applets  Applet Life Cycle  Applet Class  JAR files.
Programming in Java CSCI-2220 Object Oriented Programming.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
1  lecture slides online
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Java Applets.
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Java Applets.
Java Applets.
Java applets 1/3/2019.
11.1 Applets & graphics.
We also look at html, since we need it to use applets.
Drawing Shapes (Graphics Class) Creating Objects Packages
Presentation 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). 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);

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) {...}... }

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.

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 a reference to an object of that type. 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

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)...

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

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(); }

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 or JApplet 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).

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.

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 javax.swing.JApplet; public class AppletStarter extends JApplet { private Driver driver; public void start() { driver = new Driver(); } 1) Include the following applet in the program directory. <applet code=“AppletStarter.class” width=600 height=500> 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