Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Classes II.

Similar presentations


Presentation on theme: "Defining Classes II."— Presentation transcript:

1 Defining Classes II

2 Today’s topics Static methods Static variables Wrapper classes
References Classes as parameters to functions Copy constructor

3 Static methods and variables

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

5 Static methods Does not have/require a calling object (class instance). can refer to static members can’t refer to non static members recall members = data + functions or members = attributes + methods Outside of class, use the class name to refer to static members.

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

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

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 = minimum(a, b); //legal? if (a>=b) return a;

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

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

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

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

13 Static methods Within a class, can:
A static method call another static method? A static method call a non static method directly? A non static method call a static method? A non static method call a non static method? A static method create an instance of the class? A static method create an instance of the class and then use that instance to call a non static method?

14 Static methods Does anything really use static methods?
See System class ( See Math class (

15 Static variables belongs to the class as a whole (only one copy)
not just one particular object 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?

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

17 Static variables public class Tester { public static int sVal = 12;
Convention/suggestion: Use ‘s’ for static and ‘m’ for “ordinary” class 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 ); }

18 Wrapper classes

19 Wrapper classes Primitive types Wrapper classes
not “first class” objects int, double, float, etc. for efficiency Wrapper classes Integer, Double, etc. lack “no arg” ctors may have static members

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

21 Wrapper classes Unboxing
The process of going from an object of a wrapper class to the corresponding value of a primitive type. int I = iObj.intValue();

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

23 references

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

25 References Rules: 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 }

26 Classes as parameters to functions

27 Classes as function 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?

28 Classes as function 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 );

29 Classes as function 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

30 Classes as function 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?

31 = 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.mValue ); //prints? B.mValue = 10; }

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

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

34 null null Special constant.
Used when an object variable does not yet refer to anything.

35 null null Can be an argument to a method.
Can be assigned, tested, and passed. Integer p = null; if (p == null ) { … f( 12, null );

36 Anonymous classes new Integer( 42 );
Creates an anonymous object (not assigned to any variable but it does exist). How can we test to see if anything happened?

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

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

39 Creating your own packages
There is an OS environment variable called CLASSPATH that specifies the location (directories/folders) of classes that your program uses.

40 Copy ctor

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

42 Copy constructor 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).

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

44 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!

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

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

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

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

49 Privacy leaks When private things become not so private!

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

51 Privacy leaks Let’s add an accessor to person for the birth date.
public Date getBorn ( ) { 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?

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

53 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 ( mutable or immutable?

54 Deep copy vs. shallow 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."

Similar presentations


Ads by Google