defining classes ii. today’s topics static methods static variables wrapper classes references...
TRANSCRIPT
Defining Classes IIDefining Classes II
Today’s topicsToday’s topics
Static methodsStatic methods Static variablesStatic variables Wrapper classesWrapper classes ReferencesReferences Class parametersClass parameters Copy constructorCopy constructor
Static methodsStatic methods
You’ve already seen a static method. You’ve already seen a static method. What was it? What was it?
Static methodsStatic methods
Does not have/require a calling Does not have/require a calling object (class instance).object (class instance).
– Can’t refer to non static membersCan’t refer to non static members recall members = data + functions (or recall members = data + functions (or
attributes + methods)attributes + methods)
– Use class name to refer to the non Use class name to refer to the non static method.static method.
Static methodsStatic methods
Example:Example:
class MyMath {class MyMath {public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
if (a>=b) return a;if (a>=b) return a;
return b;return b;
}}
}}
Static methodsStatic methods
class MyMath {class MyMath {
int mData;int mData;
public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
int k = mData;int k = mData;//legal?//legal?
if (a>=b) return a;if (a>=b) return a;
return b;return b;
}}
}}
Static methodsStatic methods
class MyMath {class MyMath {
public static int minimum ( int a, int b ) {public static int minimum ( int a, int b ) {
if (a<=b)if (a<=b) return a;return a;
return b;return b;
}}
public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
int k = minimum(a, b);int k = minimum(a, b); //legal?//legal?
if (a>=b) return a;if (a>=b) return a;
return b;return b;
}}
}}
Static methodsStatic methods
class MyMath {class MyMath {
public static int minimum ( int a, int b ) {public static int minimum ( int a, int b ) {
if (a<=b)if (a<=b) return a;return a;
return b;return b;
}}
public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
int k = int k = MyMath.MyMath.minimum(a, b);minimum(a, b); //legal?//legal?
if (a>=b) return a;if (a>=b) return a;
return b;return b;
}}
}}
Static methodsStatic methods
class MyMath {class MyMath {
int mData;int mData;
public int f ( ) {public int f ( ) {
return mData;return mData;
}}
public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
int k = f();int k = f(); //legal?//legal?
if (a>=b) return a;if (a>=b) return a;
return b;return b;
}}
}}
Static methodsStatic methods
class MyMath {class MyMath {int mData;int mData;public int f ( ) {public int f ( ) {
return mData;return mData;}}public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
MyMathMyMath mm = new MyMath();mm = new MyMath();int k = int k = mm.mm.f();f(); //legal?//legal?if (a>=b) return a;if (a>=b) return a;return b;return b;
}}}}
Static methodsStatic methods
class MyMath {class MyMath {
int mData;int mData;
public int f ( ) {public int f ( ) {
return mData;return mData;
}}
public static int maximum ( int a, int b ) {public static int maximum ( int a, int b ) {
int k = int k = this.this.f();f(); //legal?//legal?
if (a>=b) return a;if (a>=b) return a;
return b;return b;
}}
}}
Static methodsStatic methods
Within a class, can:Within a class, can:1.1. A static method call another static A static method call another static
method?method?2.2. A static method call a non static method A static method call a non static method
directly?directly?3.3. A non static method call a static method?A non static method call a static method?4.4. A non static method call a non static A non static method call a non static
method?method?5.5. A static method create an instance of the A static method create an instance of the
class?class?6.6. A static method create an instance of the A static method create an instance of the
class and then use that instance to call a class and then use that instance to call a non static method?non static method?
Static methodsStatic methods
Does anything really use static Does anything really use static methods?methods?– See System class (See System class (
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html))
– See Math class (See Math class (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html))
Static variablesStatic variables
belongs to the class as a whole – not belongs to the class as a whole – not just one object (not only one copy)just one object (not only one copy)
can be used to communicate can be used to communicate between objectsbetween objects
one object changes it and it changes one object changes it and it changes for every objectfor every object
automatically initialized (to 0, false, automatically initialized (to 0, false, null)null)
also useful for defining constantsalso useful for defining constants– What keyword do we use for this?What keyword do we use for this?
Static variablesStatic variables
We stated, “one object changes it We stated, “one object changes it and it changes for every object.”and it changes for every object.”
How can we test this to see if it’s How can we test this to see if it’s true?true?
Static variablesStatic variables
public class Tester {public class Tester {public static int sVal = 12;public static int sVal = 12;public int mVal = 12;public int mVal = 12;public static void main ( String args[] )public static void main ( String args[] ){{
Tester t1 = new Tester();Tester t1 = new Tester();Tester t2 = new Tester();Tester t2 = new Tester();t1.sVal = 10;t1.sVal = 10;t1.mVal = 10;t1.mVal = 10;System.out.println( t2.sVal ); //what prints?System.out.println( t2.sVal ); //what prints?System.out.println( t2.mVal );System.out.println( t2.mVal );
}}}}
Use ‘s’ for static and ‘m’ for “ordinary” class variables.
Wrapper classesWrapper classes
Primitive typesPrimitive types– Not “first class” objectsNot “first class” objects– int, double, float, etc.int, double, float, etc.
Wrapper classesWrapper classes– Integer, Double, etc.Integer, Double, etc.– Lack “no argument” ctorsLack “no argument” ctors– May also have static membersMay also have static members
Wrapper classesWrapper classes
BoxingBoxing– The process of going from a value of a The process of going from a value of a
primitive type to an object of its wrapper primitive type to an object of its wrapper class.class.
– Integer iObj = new Integer( 42 );Integer iObj = new Integer( 42 ); UnboxingUnboxing
– The process of going from an object of a The process of going from an object of a wrapper class to the corresponding wrapper class to the corresponding value of a primitive type.value of a primitive type.
– int I = iObj.intValue();int I = iObj.intValue();
Wrapper classesWrapper classes
EveryEvery primitive type has a primitive type has a corresponding wrapper class.corresponding wrapper class.
Automatic boxing and unboxingAutomatic boxing and unboxingInteger iObj = 10; //instead of new Integer(10)Integer iObj = 10; //instead of new Integer(10)
int i = iObj; //unboxint i = iObj; //unbox
Double price = 19.90;Double price = 19.90;price = price + 1.52;price = price + 1.52;
How can we determine what’s faster (double vs. How can we determine what’s faster (double vs. Double)?Double)?
ReferencesReferences
Variables are stored in consecutive Variables are stored in consecutive bytes of memory.bytes of memory.
Q: Where is the int value of 10 for i Q: Where is the int value of 10 for i located at?located at?– A: Starting at some memory location or A: Starting at some memory location or
address.address. So variables can be referred to by So variables can be referred to by
their value (copy) or by their their value (copy) or by their reference (address).reference (address).
ReferencesReferences
In Java, variables of object types are In Java, variables of object types are references (addresses); primitive variables references (addresses); primitive variables are values (copies).are values (copies).
public class Tester3 {public class Tester3 {
public static void main ( String args[] )public static void main ( String args[] )
{{
Tester3 t = new Tester3();Tester3 t = new Tester3();
System.out.println( t ); //prints [email protected]( t ); //prints Tester3@16f0472
}}
}}
Class parametersClass parameters
Passing parameters to functions:Passing parameters to functions:– Primitive types are passed (call) by Primitive types are passed (call) by
value.value.– Class types are passed (call) by Class types are passed (call) by
reference.reference.
– How can we determine if this is true?How can we determine if this is true?
Class parametersClass parameters
public class Tester4 {public class Tester4 {int mValue = 10;int mValue = 10;public static void f1 ( int value ) {public static void f1 ( int value ) {
value = 12;value = 12;}}public static void f2 ( Tester4 t ) {public static void f2 ( Tester4 t ) {
t.mValue = 12;t.mValue = 12;}}public static void main ( String args[] )public static void main ( String args[] ){{
Tester4 t = new Tester4();Tester4 t = new Tester4();f1( t.mValue );f1( t.mValue );System.out.println( t.mValue );System.out.println( t.mValue ); //prints?//prints?f2( t );f2( t );System.out.println( t.mValue );System.out.println( t.mValue ); //prints?//prints?
}}}}
Class parametersClass parameters
public class Tester4 {public class Tester4 {int mValue = 10;int mValue = 10;public static void f1 ( int value ) {public static void f1 ( int value ) {
value = 12;value = 12;}}public static void f2 ( Tester4 t ) {public static void f2 ( Tester4 t ) {
t.mValue = 12;t.mValue = 12;}}public static void main ( String args[] )public static void main ( String args[] ){{
Tester4 t = new Tester4();Tester4 t = new Tester4();f1( t.mValue );f1( t.mValue );System.out.println( t.mValue );System.out.println( t.mValue ); //prints?//prints? 1010f2( t );f2( t );System.out.println( t.mValue );System.out.println( t.mValue ); //prints?//prints? 1212
}}}}
Class parametersClass parameters
= and == for variables of class type= and == for variables of class typeA = B makes A refer to B (also).A = B makes A refer to B (also).
A and B both now refer to the same A and B both now refer to the same object.object.
A == B is true when A and B both refer to A == B is true when A and B both refer to the same object (at the same memory the same object (at the same memory location).location).
How can we test this?How can we test this?
= and === and ==
public class Tester5 {public class Tester5 {int mValue = 12;int mValue = 12;public static void main ( String args[] )public static void main ( String args[] ){{
Tester5 A = new Tester5();Tester5 A = new Tester5();Tester5 B = new Tester5();Tester5 B = new Tester5();System.out.println( A==B );System.out.println( A==B ); //prints?//prints?A = B;A = B;System.out.println( A==B );System.out.println( A==B ); //prints?//prints?System.out.println( A.mValue ); //prints?System.out.println( A.mValue ); //prints?B.mValue = 10;B.mValue = 10;System.out.println( A.mValue ); //prints?System.out.println( A.mValue ); //prints?
}}}}
= and === and ==
public class Tester5 {public class Tester5 {int mValue = 12;int mValue = 12;public static void main ( String args[] )public static void main ( String args[] ){{
Tester5 A = new Tester5();Tester5 A = new Tester5();Tester5 B = new Tester5();Tester5 B = new Tester5();System.out.println( A==B );System.out.println( A==B ); //prints?//prints? falsefalseA = B;A = B;System.out.println( A==B );System.out.println( A==B ); //prints?//prints? truetrueSystem.out.println( A.mValue ); //prints? System.out.println( A.mValue ); //prints? 1212B.mValue = 10;B.mValue = 10;System.out.println( A.mValue ); //prints? System.out.println( A.mValue ); //prints? 1010
}}}}
= and === and ==
So why do some classes have an So why do some classes have an equals() method?equals() method?
nullnull
nullnull– Special constantSpecial constant– Used when an object variable does not Used when an object variable does not
yet refer to anything.yet refer to anything.– Can be an argument to a method.Can be an argument to a method.– Can be assigned, tested, and passed.Can be assigned, tested, and passed.
Integer p = null;Integer p = null;……if (p == null ) { …if (p == null ) { ………f( 12, null );f( 12, null );
Anonymous classesAnonymous classes
new Integer( 42 );new Integer( 42 );– Creates an anonymous object (not Creates an anonymous object (not
assigned to any variable but it does assigned to any variable but it does exist).exist).
Import statementImport statement
You can use a class from a package You can use a class from a package in any program or class definition.in any program or class definition.
Directories (folders) need not be the Directories (folders) need not be the same.same.
import Package_name.Class_name;import Package_name.Class_name; Ex.Ex.
import java.util.Scanner;import java.util.Scanner;
import java.util.*;import java.util.*;
Creating your own packagesCreating your own packages
To make a packages, group all the To make a packages, group all the classes together into a single classes together into a single directory (folder) and add the directory (folder) and add the following to the beginning of each following to the beginning of each class file:class file:package Package_name;package Package_name;
There is an OS environment variable There is an OS environment variable called CLASSPATH that specifies the called CLASSPATH that specifies the location (directories/folders) of location (directories/folders) of classes that your program uses.classes that your program uses.
Copy constructorCopy constructor
Ctor w/ a single argument of the same Ctor w/ a single argument of the same type as the class.type as the class.
Should create an object that is a Should create an object that is a separate, independent object that is separate, independent object that is an exact copy of the argument object.an exact copy of the argument object.
Using a copy ctor:Using a copy ctor:Date d1 = new Date( “January”, 1, 2006 );Date d1 = new Date( “January”, 1, 2006 );Date d2 = new Date( d1 ); //copy ctorDate d2 = new Date( d1 ); //copy ctor– Changes to d1 should not modify d2 (and Changes to d1 should not modify d2 (and
vice versa).vice versa).
Copy ctor exampleCopy ctor example
public class Date {public class Date {……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;
}}……
}}
Copy ctorCopy ctor
Recall:Recall:– Should create an object that is a separate, Should create an object that is a separate,
independent object that is an exact copy independent object that is an exact copy of the argument object.of the argument object.
– Let’s see an example that violates this.Let’s see an example that violates this.
Bad copy ctorBad copy ctorpublic class Date {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 {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( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints?
Person p2 = new Person( p1 );Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 );p2.mBorn.setYear( 5000 ); System.out.println( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints? }}}}
Bad copy ctorBad copy ctorpublic class Date {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 {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( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints? 20002000
Person p2 = new Person( p1 );Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 );p2.mBorn.setYear( 5000 ); System.out.println( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints? 50005000 }}}}
Good copy ctorGood copy ctorpublic class Date {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 {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 = mBorn = new Date( new Date( other.mBornother.mBorn ) );; mDied = mDied = new Date( new Date( other.mDiedother.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( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints?
Person p2 = new Person( p1 );Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 );p2.mBorn.setYear( 5000 ); System.out.println( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints? }}}}
Good copy ctorGood copy ctorpublic class Date {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 {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 = mBorn = new Date( new Date( other.mBornother.mBorn ) );; mDied = mDied = new Date( new Date( other.mDiedother.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( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints? 20002000
Person p2 = new Person( p1 );Person p2 = new Person( p1 ); p2.mBorn.setYear( 5000 );p2.mBorn.setYear( 5000 ); System.out.println( System.out.println( p1p1.mBorn.getYear() );.mBorn.getYear() );
//prints?//prints? 20002000 }}}}
Privacy leaksPrivacy leaks
When private things become not so When private things become not so private!private!
Privacy leaksPrivacy leaks
Let’s add an accessor to person for Let’s add an accessor to person for the birth date.the birth date.
public Date getBorn ( ) {public Date getBorn ( ) {
return mBorn;return mBorn;
}}
Why is this bad?Why is this bad?
Privacy leaksPrivacy leaks
Let’s add an accessor to person for the Let’s add an accessor to person for the birth date.birth date.
public Date getBorn ( ) {public Date getBorn ( ) {
return mBorn;return mBorn;
}}
Why is this bad? Why is this bad? Because we now get use Because we now get use getBorn to get a reference to the private getBorn to get a reference to the private date and then modify it.date and then modify it.
What’s the fix?What’s the fix?
Privacy leaksPrivacy leaks
Let’s add an accessor to person for Let’s add an accessor to person for the birth date.the birth date.
public Date getBorn ( ) {public Date getBorn ( ) { return return new Date(mBorn)new Date(mBorn);; }}
This gives us a copy of the date This gives us a copy of the date (which we can modify) but not the (which we can modify) but not the date associated with the person.date associated with the person.
Mutable and immutable classesMutable and immutable classes Immutable class = doesn’t contain any Immutable class = doesn’t contain any
methods that change any of the data in an methods that change any of the data in an object of the classobject of the class
Mutable class = contains public mutator Mutable class = contains public mutator methods or other public methods that can methods or other public methods that can change the data in an object of the classchange the data in an object of the class– Rule: Never return a reference to a mutable Rule: Never return a reference to a mutable
private object.private object. Is the String class (Is the String class (
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html) mutable or immutable?) mutable or immutable?
Deep copy vs. shallow copyDeep copy vs. shallow copy
Deep copyDeep copy– Copy that, with one exception, has no Copy that, with one exception, has no
references in common with the original references in common with the original objectobject
– Exception: references to immutable Exception: references to immutable objects are allowed to be sharedobjects are allowed to be shared
Shallow copy = a copy that is not Shallow copy = a copy that is not deepdeep