Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Value vs. reference semantics. Recall: Value semantics value semantics: Behavior where variables are copied when assigned to each other or passed as.
Introduction to Programming Lecture 39. Copy Constructor.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 5 Defining Classes II Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.
CMSC 202 Classes and Objects: Reusing Classes with Composition.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 1201 Object Oriented Programming ArrayList 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,
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Static Methods Slides 2 to 15 only Static Methods A static method is one that can be used without a calling object A static method still belongs.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Defining Classes II
More About Objects and Methods
Topic: Classes and Objects
Static Members and Methods
Java Primer 1: Types, Classes and Operators
Chapter 5 Defining Classes II
CMSC 202 Static Methods.
CSC240 Computer Science III
Computer Science II Exam 1 Review.
Defining Classes II.
An Introduction to Java – Part II
Chapter 5 Defining Classes II
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Defining Classes I Part A.
Chapter 5 Defining Classes II
More About Objects and Methods
Assessment – Java Basics: Part 1
Java Classes and Objects 4th Lecture
Chapter 5 Defining Classes II
Classes and Objects Static Methods
Outline Creating Objects The String Class The Random and Math Classes
Classes and Objects Reusing Classes with Composition
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Defining Classes II

Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor

Static methods  You’ve already seen a static method. What was it?

Static methods  Does not have/require a calling object (class instance). –Can’t refer to non static members  recall members = data + functions (or attributes + methods) –Use class name to refer to the non static method.

Static methods Example: class MyMath { public static int maximum ( int a, int b ) { if (a>=b) return a; return b; }}

Static methods class MyMath { int mData; public static int maximum ( int a, int b ) { int k = mData;//legal? if (a>=b) return a; return b; }}

Static methods class MyMath { public static int minimum ( int a, int b ) { if (a<=b)return a; return b; } public static int maximum ( int a, int b ) { int k = minimum(a, b);//legal? if (a>=b) return a; return b; }}

Static methods class MyMath { public static int minimum ( int a, int b ) { if (a<=b)return a; return b; } public static int maximum ( int a, int b ) { int k = MyMath.minimum(a, b);//legal? if (a>=b) return a; return b; }}

Static methods class MyMath { int mData; public int f ( ) { return mData; } public static int maximum ( int a, int b ) { int k = f();//legal? if (a>=b) return a; return b; }}

Static methods class MyMath { int mData; public int f ( ) { return mData; } public static int maximum ( int a, int b ) { MyMathmm = new MyMath(); int k = mm.f();//legal? if (a>=b) return a; return b; }}

Static methods class MyMath { int mData; public int f ( ) { return mData; } public static int maximum ( int a, int b ) { int k = this.f();//legal? if (a>=b) return a; return b; }}

Static methods  Within a class, can: 1. A static method call another static method? 2. A static method call a non static method directly? 3. A non static method call a static method? 4. A non static method call a non static method? 5. A static method create an instance of the class? 6. A static method create an instance of the class and then use that instance to call a non static method?

Static methods  Does anything really use static methods? –See System class ( pi/java/lang/System.html) pi/java/lang/System.htmlhttp://java.sun.com/j2se/1.5.0/docs/a pi/java/lang/System.html –See Math class ( pi/java/lang/Math.html) pi/java/lang/Math.htmlhttp://java.sun.com/j2se/1.5.0/docs/a pi/java/lang/Math.html

Static variables  belongs to the class as a whole – not just one object (not only one copy)  can be used to communicate between objects  one object changes it and it changes for every object  automatically initialized (to 0, false, null)  also useful for defining constants –What keyword do we use for this?

Static variables  We stated, “one object changes it and it changes for every object.”  How can we test this to see if it’s true?

Static variables public class Tester { public static int sVal = 12; public int mVal = 12; public static void main ( String args[] ) { Tester t1 = new Tester(); Tester t2 = new Tester(); t1.sVal = 10; t1.mVal = 10; System.out.println( t2.sVal ); //what prints? System.out.println( t2.mVal ); }} Use ‘s’ for static and ‘m’ for “ordinary” class variables.

Wrapper classes  Primitive types –Not “first class” objects –int, double, float, etc.  Wrapper classes –Integer, Double, etc. –Lack “no argument” ctors –May also have static members

Wrapper classes  Boxing –The process of going from a value of a primitive type to an object of its wrapper class. –Integer iObj = new Integer( 42 );  Unboxing –The process of going from an object of a wrapper class to the corresponding value of a primitive type. –int I = iObj.intValue();

Wrapper classes  Every primitive type has a corresponding wrapper class.  Automatic boxing and unboxing Integer iObj = 10; //instead of new Integer(10) int i = iObj; //unbox Double price = 19.90; price = price ; How can we determine what’s faster (double vs. Double)?

References  Variables are stored in consecutive bytes of memory.  Q: Where is the int value of 10 for i located at? –A: Starting at some memory location or address.  So variables can be referred to by their value (copy) or by their reference (address).

References  In Java, variables of object types are references (addresses); primitive variables are values (copies). public class Tester3 { public static void main ( String args[] ) { Tester3 t = new Tester3(); System.out.println( t ); //prints }}

Class parameters  Passing parameters to functions: –Primitive types are passed (call) by value. –Class types are passed (call) by reference. –How can we determine if this is true?

Class parameters public class Tester4 { int mValue = 10; public static void f1 ( int value ) { value = 12; } public static void f2 ( Tester4 t ) { t.mValue = 12; } public static void main ( String args[] ) { Tester4 t = new Tester4(); f1( t.mValue ); System.out.println( t.mValue );//prints? f2( t ); System.out.println( t.mValue );//prints? }}

Class parameters public class Tester4 { int mValue = 10; public static void f1 ( int value ) { value = 12; } public static void f2 ( Tester4 t ) { t.mValue = 12; } public static void main ( String args[] ) { Tester4 t = new Tester4(); f1( t.mValue ); System.out.println( t.mValue );//prints?10 f2( t ); System.out.println( t.mValue );//prints?12 }}

Class parameters  = and == for variables of class type A = B makes A refer to B (also). A and B both now refer to the same object. A == B is true when A and B both refer to the same object (at the same memory location). How can we test this?

= and == public class Tester5 { int mValue = 12; public static void main ( String args[] ) { Tester5 A = new Tester5(); Tester5 B = new Tester5(); System.out.println( A==B );//prints? A = B; System.out.println( A==B );//prints? System.out.println( A.mValue ); //prints? B.mValue = 10; System.out.println( A.mValue ); //prints? }}

= and == public class Tester5 { int mValue = 12; public static void main ( String args[] ) { Tester5 A = new Tester5(); Tester5 B = new Tester5(); System.out.println( A==B );//prints?false A = B; System.out.println( A==B );//prints?true System.out.println( A.mValue ); //prints? 12 B.mValue = 10; System.out.println( A.mValue ); //prints? 10 }}

= and ==  So why do some classes have an equals() method?

null  null –Special constant –Used when an object variable does not yet refer to anything. –Can be an argument to a method. –Can be assigned, tested, and passed. Integer p = null; … if (p == null ) { … … f( 12, null );

Anonymous classes  new Integer( 42 ); –Creates an anonymous object (not assigned to any variable but it does exist).

Import statement  You can use a class from a package in any program or class definition.  Directories (folders) need not be the same.  import Package_name.Class_name;  Ex. import java.util.Scanner; import java.util.*;

Creating your own packages  To make a packages, group all the classes together into a single directory (folder) and add the following to the beginning of each class file: package Package_name;  There is an OS environment variable called CLASSPATH that specifies the location (directories/folders) of classes that your program uses.

Copy constructor  Ctor w/ a single argument of the same type as the class.  Should create an object that is a separate, independent object that is an exact copy of the argument object.  Using a copy ctor: Date d1 = new Date( “January”, 1, 2006 ); Date d2 = new Date( d1 ); //copy ctor –Changes to d1 should not modify d2 (and vice versa).

Copy ctor example public class Date { … public Date ( Date other ) { mMonth = other.mMonth; mDay = other.mDay; mYear = other.mYear; }…}

Copy ctor  Recall: –Should create an object that is a separate, independent object that is an exact copy of the argument object. –Let’s see an example that violates this.

Bad copy ctor public class Date { private String mMonth; private String mMonth; private int mDay; private int mDay; private int mYear; private int mYear; public Date ( ) { public Date ( ) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1900; mYear = 1900; } public Date ( Date other ) { public Date ( Date other ) { mMonth = other.mMonth; mMonth = other.mMonth; mDay = other.mDay; mDay = other.mDay; mYear = other.mYear; mYear = other.mYear; } public void setYear ( int whatYear ) { public void setYear ( int whatYear ) { mYear = whatYear; mYear = whatYear; } public int getYear ( ) { public int getYear ( ) { return mYear; return mYear; }} public class Person { private Date mBorn; private Date mBorn; private Date mDied; private Date mDied; public Person ( ) { public Person ( ) { mBorn = mDied = null; mBorn = mDied = null; } public Person ( Person other ) { public Person ( Person other ) { mBorn = other.mBorn; mBorn = other.mBorn; mDied = other.mDied; mDied = other.mDied; } public static void main ( String args[] ) { public static void main ( String args[] ) { Person p1 = new Person(); Person p1 = new Person(); p1.mBorn = new Date(); p1.mBorn = new Date(); p1.mBorn.setYear( 2000 ); p1.mBorn.setYear( 2000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() );//prints? Person p2 = new Person( p1 ); Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 ); p2.mBorn.setYear( 5000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() );//prints? }}

Bad copy ctor public class Date { private String mMonth; private String mMonth; private int mDay; private int mDay; private int mYear; private int mYear; public Date ( ) { public Date ( ) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1900; mYear = 1900; } public Date ( Date other ) { public Date ( Date other ) { mMonth = other.mMonth; mMonth = other.mMonth; mDay = other.mDay; mDay = other.mDay; mYear = other.mYear; mYear = other.mYear; } public void setYear ( int whatYear ) { public void setYear ( int whatYear ) { mYear = whatYear; mYear = whatYear; } public int getYear ( ) { public int getYear ( ) { return mYear; return mYear; }} public class Person { private Date mBorn; private Date mBorn; private Date mDied; private Date mDied; public Person ( ) { public Person ( ) { mBorn = mDied = null; mBorn = mDied = null; } public Person ( Person other ) { public Person ( Person other ) { mBorn = other.mBorn; mBorn = other.mBorn; mDied = other.mDied; mDied = other.mDied; } public static void main ( String args[] ) { public static void main ( String args[] ) { Person p1 = new Person(); Person p1 = new Person(); p1.mBorn = new Date(); p1.mBorn = new Date(); p1.mBorn.setYear( 2000 ); p1.mBorn.setYear( 2000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() ); //prints?2000 Person p2 = new Person( p1 ); Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 ); p2.mBorn.setYear( 5000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() ); //prints?5000 }}

Good copy ctor public class Date { private String mMonth; private String mMonth; private int mDay; private int mDay; private int mYear; private int mYear; public Date ( ) { public Date ( ) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1900; mYear = 1900; } public Date ( Date other ) { public Date ( Date other ) { if (other==null) { if (other==null) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1900; mYear = 1900; } else { } else { mMonth = other.mMonth; mMonth = other.mMonth; mDay = other.mDay; mDay = other.mDay; mYear = other.mYear; mYear = other.mYear; } } public void setYear ( int whatYear ) { public void setYear ( int whatYear ) { mYear = whatYear; mYear = whatYear; } public int getYear ( ) { public int getYear ( ) { return mYear; return mYear; }} public class Person { private Date mBorn; private Date mBorn; private Date mDied; private Date mDied; public Person ( ) { public Person ( ) { mBorn = mDied = null; mBorn = mDied = null; } public Person ( Person other ) { public Person ( Person other ) { mBorn = new Date( other.mBorn ); mBorn = new Date( other.mBorn ); mDied = new Date( other.mDied ); mDied = new Date( other.mDied ); } public static void main ( String args[] ) { public static void main ( String args[] ) { Person p1 = new Person(); Person p1 = new Person(); p1.mBorn = new Date(); p1.mBorn = new Date(); p1.mBorn.setYear( 2000 ); p1.mBorn.setYear( 2000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() );//prints? Person p2 = new Person( p1 ); Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 ); p2.mBorn.setYear( 5000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() );//prints? }}

Good copy ctor public class Date { private String mMonth; private String mMonth; private int mDay; private int mDay; private int mYear; private int mYear; public Date ( ) { public Date ( ) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1900; mYear = 1900; } public Date ( Date other ) { public Date ( Date other ) { if (other==null) { if (other==null) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1900; mYear = 1900; } else { } else { mMonth = other.mMonth; mMonth = other.mMonth; mDay = other.mDay; mDay = other.mDay; mYear = other.mYear; mYear = other.mYear; } } public void setYear ( int whatYear ) { public void setYear ( int whatYear ) { mYear = whatYear; mYear = whatYear; } public int getYear ( ) { public int getYear ( ) { return mYear; return mYear; }} public class Person { private Date mBorn; private Date mBorn; private Date mDied; private Date mDied; public Person ( ) { public Person ( ) { mBorn = mDied = null; mBorn = mDied = null; } public Person ( Person other ) { public Person ( Person other ) { mBorn = new Date( other.mBorn ); mBorn = new Date( other.mBorn ); mDied = new Date( other.mDied ); mDied = new Date( other.mDied ); } public static void main ( String args[] ) { public static void main ( String args[] ) { Person p1 = new Person(); Person p1 = new Person(); p1.mBorn = new Date(); p1.mBorn = new Date(); p1.mBorn.setYear( 2000 ); p1.mBorn.setYear( 2000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() ); //prints?2000 Person p2 = new Person( p1 ); Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 ); p2.mBorn.setYear( 5000 ); System.out.println( p1.mBorn.getYear() ); System.out.println( p1.mBorn.getYear() ); //prints?2000 }}

Privacy leaks  When private things become not so private!

Privacy leaks  Let’s add an accessor to person for the birth date. public Date getBorn ( ) { public Date getBorn ( ) { return mBorn; return mBorn; }  Why is this bad?

Privacy leaks  Let’s add an accessor to person for the birth date. public Date getBorn ( ) { public Date getBorn ( ) { return mBorn; return mBorn; }  Why is this bad? Because we now get use getBorn to get a reference to the private date and then modify it.  What’s the fix?

Privacy leaks  Let’s add an accessor to person for the birth date. public Date getBorn ( ) { public Date getBorn ( ) { return new Date(mBorn); return new Date(mBorn); }  This gives us a copy of the date (which we can modify) but not the date associated with the person.

Mutable and immutable classes  Immutable class = doesn’t contain any methods that change any of the data in an object of the class  Mutable class = contains public mutator methods or other public methods that can change the data in an object of the class –Rule: Never return a reference to a mutable private object.  Is the String class ( java/lang/String.html) mutable or immutable? java/lang/String.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/ java/lang/String.html

Deep copy vs. shallow copy  Deep copy –Copy that, with one exception, has no references in common with the original object –Exception: references to immutable objects are allowed to be shared  Shallow copy = a copy that is not deep