Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.

Slides:



Advertisements
Similar presentations
Basic Java Constructs and Data Types – Nuts and Bolts
Advertisements

Introduction to Computation and Problem
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
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.
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Object-Oriented Programming with Java Java with added Swing Lecture 3.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
© Marty Hall, Larry Brown Web core programming 1 Basic Object-Oriented Programming in Java.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
From BlueJ to NetBeans SWC 2.semester.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
CSC 142 Computer Science II Zhen Jiang West Chester University
CSC Programming I Lecture 6 September 4, 2002.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 7: Class Variables and Methods Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Class.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 1 CS100A, Lecture 7, 22 Sept Static fields and methods. Suppose that we would like to main-
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
CS100A, Lecture 7, 22 Sept Static fields and methods
First Programs CSE 1310 – Introduction to Computers and Programming
CSC240 Computer Science III
Java Programming with BlueJ
Classes & Objects: Examples
Implementing Classes Chapter 3.
Simple Classes in Java CSCI 392 Classes – Part 1.
Visibilities and Static-ness
Presentation transcript:

Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward

Goals This the session in which we explain what static means. You will learn how to write classes with static methods and data members. You will modify a simple class using the BlueJ IDE. 2

The UnitCircle Program public class UnitCircle { public static void main ( String [ ] args ) { double circum = 2 * Math.PI; System.out.println( "Circumference of the unit circle = " + circum ); } 3

Math.PI double circum = 2 * Math.PI; What is Math? Class, object, or method? How do you know? Can you create Math instances? What is PI? Class, data member, or method? It's defined as public static final double PI = ; – final makes it read only 4

Why Math.PI is static If PI is a data member, what object does it belong to? We can't create Math instances so it can't belong to an instance of the Math class. Because PI is declared static, it belongs to the Math class object. Access it using the class name, not an object reference. Yes, even classes are objects. How many Math class objects are there in any Java® program? How many PI data members? 5

System.out.println() System.out.println( "Circumference= " + circum ); What is System? Class, object or method? Can you create an instance of System? What is out? Class, object, data member, method? What is println ()? In what class do you think println () is declared? How do you think System.out is declared? 6

System.out public final static Print Stream out; public so that we can print to it from any object final so that the user can't replace it – You can not replace a final object reference, but you can modify the object referred to. Every time we call println() we modify out. static so that there will be exactly one output connection to the user's terminal window PrintStream because that class has all the right methods 7

Classes Without Instances (Preview) How do you write a class that can not have instances? – Declare it to be abstract public abstract class NoInstanceClass {... } 8

Classes Without Instances (Preview) – Make the default constructor private public class NoInstanceClass { private NoInstanceClass () {} } – If a class has no constructor, Java® will supply a default constructor – But if you supply any constructor, then Java® won't create one. – If you define only one constructor and make it private, you can't create an instance outside the class. 9

When Do static Members get Initialized? Just before they are used. In general the compiler takes care of this. But the compiler can't resolve paradox: public class Init { // Evil public static final int i1 = 2*i2; public static final int i2 = 2*i1; } 10

static Colors Java® has a Color class that we will use extensively when we create graphic user interfaces. The usual way to create a new Color is to use a constructor that takes a red, green and blue component: Color tangerine = new Color( 255, 100, 50 ); Color also defines certain frequently used colors as static so you don’t need to create them. For instance, Color.red is defined as public final static Color red = new Color (255, 0, 0); 11

Summary of static Data Members Belong to the class, not an instance of the class so there is only one copy Typically public so you can access them using MyClass.myStatic Often final so they can not be “changed” Typically uses: – Defined constants: Math.PI, Color.red – Fixed resources: System.out 12

static Methods Normal methods are called on an instance of a particular class. A good example from PS2 is the getVolume() method that you were asked to write as part of the Tank class. It used the tank radius and length given as constructor arguments to calculate and return the tank volume. Tank t = new Tank( 5.0, 20.0 ); System.out.println( t.getVolume() ); static methods are not called on an instance. Instead they are conceptually called on the class. 13

Example Methods Why does the Java® entry point method public static void main( String [ ] args ) have to be static. Why is Math.pow( double a, double b ) static? Why is int Integer.parseInt( String s ) static? Why isn’t the int intValue() method in Integer static? Why isn’t System.out.println( String s ) static? 14

The Syntax of static Methods Defining: // static public static int parseInt( String s ) // instance public int intValue(); Calling: int ival = Integer.parseInt( "42" ); Integer bigI = new Integer( "42" ); ival = bigI.intValue(); 15

static Methods Can't Access Instance Data or Methods Find the errors (2): public class StaticError { private int k; public StaticError() { k = 42; } public int getValue() { return k; } public static void main( String [] args ) { int k2 = 2*k; System.out.println( "k= " + getValue() ); } 16

Instance or static as a Matter of Style Sometimes it is just a matter of style whether you declare a method to be an instance or a static method. Consider a class Work used for physics and engineering calculations. The constructor takes 2 arguments, a double and a String indicating the units of the first argument. Remember that a confusion on this issue between NASA and a contractor led to the loss of a Mars probe a few years ago. 17

class Work public class Work { public static final String JOULE = "joule"; public static final String LBF = "foot-pound"; public static final String BTU = "btu";... private double val; // in joules public Work( double w, String units ) {... } } 18

Instance or static, 2 Which is the better way to add two instances of Work? public static Work add(Work w1, Work w2); or public Work add( Work w ); 19

BlueJ Exercise Using your browser, navigate to Lecture9. and download static.zip. Double click static.zip and extract it to a new directory. Start BlueJ and navigate to the directory in which you unpacked the static.zip file. You will see two icons: an orange rectangle labeled Street and a text file icon. Double click the text file icon and proceed from there. 20 Java® is a trademark or registered trademark of Sun Microsystems, Inc. In the United States and other countries.