Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor."— Presentation transcript:

1 Defining Classes II

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

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

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

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

6 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; }}

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

8 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; }}

9 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; }}

10 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; }}

11 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; }}

12 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?

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

14 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?

15 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?

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

17 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

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

19 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 + 1.52; How can we determine what’s faster (double vs. Double)?

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

21 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 Tester3@16f0472 }}

22 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?

23 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? }}

24 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 }}

25 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?

26 = 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? }}

27 = 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 }}

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

29 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 );

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

31 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.*;

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

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

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

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

36 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? }}

37 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 }}

38 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? }}

39 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 }}

40 Privacy leaks  When private things become not so private!

41 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?

42 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?

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

44 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 (http://java.sun.com/j2se/1.5.0/docs/api/ java/lang/String.html) mutable or immutable? http://java.sun.com/j2se/1.5.0/docs/api/ java/lang/String.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/ java/lang/String.html

45 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


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

Similar presentations


Ads by Google