Lecture Static Methods and Variables. Static Methods

Similar documents
Lecture Static Methods and Variables. Static Methods

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Classes. Classes as Code Libraries. Classes as Data Structures

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

CS110: PROGRAMMING LANGUAGE I

CS335 Graphics and Multimedia

CSC System Development with Java Introduction to Java Applets Budditha Hettige

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Object Oriented Programming

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

GUI, Events and Applets from Applications, Part III

Summary. 962 Chapter 23 Applets and Java Web Start

Chapter 7 Applets. Answers

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

CT 229 Arrays in Java

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Name CS/120 Sample Exam #1 -- Riley. a) Every program has syntax, which refers to the form of the code, and, which refers to the meaning of the code.

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

CS 209 Programming in Java #12 JAR Files: Creation and Use

Chapter 5 Methods / Functions

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Programming by Delegation

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Chapter 3 - Introduction to Java Applets

Programming: You will have 6 files all need to be located in the dir. named PA4:

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Advanced Internet Programming CSY3020

Class 9: Static Methods and Data Members

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

Java Applet & its life Cycle. By Iqtidar Ali

G51PRG: Introduction to Programming Second semester Applets and graphics

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

PROGRAMMING LANGUAGE 2

Methods and Data (Savitch, Chapter 5)

Lecture 5: Methods CS2301

Object-Oriented Concepts

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

Introduction to Computer Science I

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

DUBLIN CITY UNIVERSITY

DUBLIN CITY UNIVERSITY

Programming graphics

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Java Applet Basics. Life cycle of an applet:

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Object Orientation Fourth Story. Bok, Jong Soon

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

CS1004: Intro to CS in Java, Spring 2005

Chapter 6 Methods. Dr. Hikmat Jaber

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Ch 7 Designing Java Classes & Class structure

CS506 Web Design & Development Final Term Solved MCQs with Reference

Methods. Methods. Mysteries Revealed

Fundamentals of Object Oriented Programming

CSCE3193: Programming Paradigms

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Chapter 12 Advanced GUIs and Graphics

Java Memory Management

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

JAVA: A Primer. By: Amrita Rajagopal


You should now start on Chapter 4. Chapter 4 introduces the following concepts

Building Java Programs

CS 116 Week 8 Page 1

Chapter 4: Writing Classes

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Contents 8-1. Copyright (c) N. Afshartous

Chapter 4 Java Language Fundamentals

The life and death of objects, sta2cs. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Shared Collection of Java Course Materials: New Topics Covered. Agenda

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Java 1.8 Programming

CS 302 Week 9. Jim Williams

Methods (Deitel chapter 6)

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

CSD Univ. of Crete Fall Java Applets

Computer Science II (20082) Week 1: Review and Inheritance

Methods (Deitel chapter 6)

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Introduction to Java

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 2 Using Objects. Types. Number Literals. A type defines a set of values and the operations that can be carried out on the values Examples:

EE219 Object Oriented Programming I (2005/2006) SEMESTER 1 SOLUTIONS

Programmierpraktikum

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Lecture 02, Fall 2018 Friday September 7

Primitive Data Types: Intro

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Transcription:

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 1

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 2

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. 2) Instantiating a second object of the same type does not increase the number of static variables. Example 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 ); 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 3

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 4

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 5

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 6