CS 1312 - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

CS 1312

Description:

Just because a variable might be declared as a field within a ... box1 = iLn, iWd, iHt; Hello World. str1. box1. ERROR: must use new 'new' and call constructor ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 47
Provided by: davidd1
Category:
Tags: iht

less

Transcript and Presenter's Notes

Title: CS 1312


1
CS 1312
  • Introduction to
  • Object Oriented Programming
  • Lecture 6
  • Instance versus Static, Strings, Debugging

2
Instance vs. ClassDeclarations
3
Instance vs. Class Declarations
  • Dont be fooled. Just because a variable might
    be declared as a field within a class that does
    not imply that it is a class variable.
  • It could be
  • a class variable, or
  • an instance variable!
  • Likewise, each method is indeed declared within a
    class, but that does not imply that it is a
    class method.
  • It could be
  • a class method, or
  • an instance method.
  • Class variable and class method both mean
    something very specific.

4
Instance vs. Class Declarations
  • A distinction that applies to both variables and
    methods
  • An instance ltvariable or methodgt is one that
    belongs to each object of a class.
  • A class ltvariable or methodgt is one that belongs
    only to the class itself. (N.B. It will be
    accessible to the objects)
  • The keyword static
  • indicates a class variable or class method.
  • absence of the keyword static indicates an
    instance variable or instance method.

5
Instance vs. Class Variables
Suppose we wanted to track the total number of
objects created. Consider class Human
String name int population 0 public
Human (String name) this.name
name population //WRONG! // of
constructor // of Human
Declares a name String for each instance. Thus,
each Human will have its own name. But . . .
Also declares a population counter for each
instance of Human.
Thus, each Human will have its own population
variable, each having a value of 1. This
makes no sense!
6
Instance vs. Class Variables
class Human String name static int
population 0 public Human (String name)
this.name name population //
of constructor // of Human
NOTE Each Human does not get a population
counter. This declares a single population
counter for the class Human itself. It is a
class variable. Thus, each Human will increment
this single shared counter by 1.
One change!
Each instance will still have a String name
Thus, each Human will have its own name.
7
Instance vs. Class Variables When to Use
Quiz Alert!
Use instance variables whenever each object
should have its own variable. E.g., attributes
of the particular object. name, ssn, age,
weight Use a class variable whenever
the class itself should maintain a single copy
of datum pertaining to all instances of the
class. E.g., population counts
summary data assigning lot numbers
shared resources.
8
Instance vs. Class Variables
We now see the reason behind declaring most
constants as static as well as final.
Declares a different-but-identical constantfor
each instance of the class. Wasteful with zero
benefit.
Constants Revisited class ConstantExample
final int iMAXSIZE 10 // of
ConstantExample class ConstantExample
static final int iMAXSIZE 10 // of
ConstantExample
Declares a single constant for use by all
instances of the class.
9
Quiz Yourself
Consider why the following code will not compile
  • public class Test
  • public void sayHello()
  • System.out.println (Hello)
  • public static void main (String arg)
  • sayHello()

10
Classes and Objects
  • The error produced is
  • Can't make static reference to method void
    sayHello() in class test.
  • Why? The answer lies in the difference between
    objects and classes. As noted in previous
    slides, classes are composed of member objects,
    primitives and methods. When properly designed,
    classes provide state and behavior for objects.
  • As an OO language, Java emphasizes the use of
    objects to manipulate data. Thus, we must make
    instances of a class in order to drive a program.

11
Another Perspective
t1
  • public class Test
  • public void sayHello()
  • System.out.println (Hello)
  • public static void main (String arg)
  • sayHello() // WRONG!
  • //class Test

t2
public class Test public void sayHello()
System.out.println (Hello)
public static void main (String arg)
sayHello() // WRONG! //class Test
public class Test public void sayHello()
System.out.println (Hello)
public static void main (String arg)
sayHello() // WRONG! //class Test
t3
public class Test public void sayHello()
System.out.println (Hello)
public static void main (String arg)
sayHello() // WRONG! //class Test
When we create the class Test, there are
potentially an infinite number of instances
available. Test t1 new Test() Test t2
new Test() // etc.
12
INSTANCES
CLASS
Test t1 new Test() t1.x 5 // should use
accessor
public class Test int x static int y
2 //class Test
t1
public class Test int x // now 5 static
int y // 2 //class Test
static int y
New class instances (objects) get unique copies
of every member--except for static or class
members. We can think of static as meaning only
one copy
Test t2 new Test() t2.x 3 // should use
accessor
t2
public class Test int x // now 3 static
int y // 2 //class Test
static int y
13
t1
public class Test public void sayHello()
System.out.println (Hello)
public static void main (String arg)
sayHello() // WRONG! //class Test
t2
public class Test public void sayHello()
System.out.println (Hello)
public static void main (String arg)
sayHello() // WRONG! //class Test
So, when Java encounters our class, it sees
a potentially infinite number of Test
instances, each with their own behavior
(sayHello() ), and only one (static)
main shared between all the classes
14
public class Test public void sayHello()
System.out.println (Hello)
public static void main (String arg)
sayHello() // WRONG! //class Test
Potentially many
Only one
Thus, the error of making a static reference to
a method might be understood as a problem of
ambiguity. We have not told Java which of the
many possible instances of class Test we intend
to call sayHello() on.
15
Class vs. Instance Members
The class or static members are defined for
all instances. Every instance has access to
static members, and can change them for all other
members.
INSTANCES
CLASS

class test public void
sayHello() // etc. etc
public static void main (String argv)

16
Solution I
One merely needs to resolve this ambiguity to
correct the program. Thus, just make an instance
of the class, and reference/invoke the objects
members.
This is a core concept in cs1312.
17
OR
18
Solution 2
One merely needs to eliminate this ambiguity to
correct the program. Thus, just make the method
static.
This is a core concept in cs1312.
19
Warning
You will find this course very difficult, if not
impossible, if you did not grasp the previous
slides. Misuse of static is the second most
pernicious bug found in cs1312.
20
Do you follow this?
public class Ouch int w 0 static int x
0 final int y 0 final static int z 0
public static void main(String args)
Ouch one new Ouch() Ouch two new
Ouch() Ouch three new Ouch() // see
questions // main // Ouch
How many ws are in memory? How many xs?
How many ys? How many zs?
Are any constant?
21
Little secrets
Class methods and class variables exist and can
be used even when no objects have been
instantiated! Instance methods and instance
variables do not exist on their own, but only as
part of an instance. Objects can be used to
access both class and instance members. Classes
can be used to access only class members!
22
Questions?
23
Announcements
  • Read git.cc.class.cs1312.announce every day!You
    are playing a risky game without it!

24
String-Fu
25
Strings vis-a-vis Objects
Every String is an instance of Javas built-in
class String. Thus, Strings are objects. Java
provides extra support for Strings as a
convenience because of the frequency with which
Strings are used. (Thats why they can seem like
primitives but they are not.) Three obvious
String support features 1. You need not
explicitly instantiate Strings with the new
keyword. Java automatically instantiates a
String object when it encounters a text
string within double-quotes. For example .
. .
26
Strings vis-a-vis Objects
Assignment w/References to Strings/Objects
Code
Memory
String str1 Box box1
str1 box1
27
Important Digression
  • Given
  • String s new String()
  • How can we be sure of the default behavior?
  • Remember that thing - the API? Download it!
  • (http//www.javasoft.com/docs)
  • Go to class String
  • Click on constructors
  • Look for the default one and read its
    documentation
  • Mystery solved!
  • Now back to the show..

28
Strings vis-a-vis Objects
Again, Java automatically creates a new String
object whenever it encounters text within
double-quote marks. Thus, the code
String str1 Hello World accomplishes
three things 1. It creates str1 as a
reference to a String. 2. It creates an
instance of a String. 3. It initializes that
String to Hello World. This is inconsistent
with how Java treats standard objects. With
standard objects, you must explicitly
instantiate (via new), and initialize (via a
constructor).
29
String Stuff
Three obvious String support features 1. You
need not explicitly instantiate Strings.
2. The operater overloaded for Strings, to
support concatenation, e.g.,
System.out.println(This string is an example of
one that is too long to fit on one
line. Your TAs take off points
for lines that exceed 80 column characters.)
3. Several predefined methods provided in
built-in class String. Among them are
length( ) // a string knows
its length note it is a method!
charAt(iIndex) // returns letter at
position iIndex 1st char is position 0
substring(iStartIndex) // returns the substring
fromposition // iStartIndex to end
of string substring(iStartIndex, iEndIndex) //
returns substring from position
// iStartIndex until but NOT INCLUDING
position iEndIndex
continued
30
String Stuff -- Examples
String strExample Hello
char c strExample.charAt(1) // c gets
e String strBritishHowdy strExample.substring
(1) strBritishHowdy ---gt
ello String strTemperatureOutside
strExample.substring(0, 4)
strTemperatureOutside --gt Hell
The second argument is length, not a reference to
index values
31
Strings vis-a-vis Objects
  • Also . . .
  • One cannot change contents of a String object.
    (We say Strings are immutable.)
  • You may think you are modifying a String. But,
    what happens
  • in memory is
  • a new String is created
  • you change the String reference to refer to that
    new one
  • your old String may be garbage collected you
    no longer
  • have a reference to the old String
  • For example

str1
Hello World
String str1 Hello World str1
str1.substring(4)
str1
Hello World
o World
Optional see class StringBuffer for ways to work
around this limitation.
32
Objects and References The special case of String
  • Caution In the next few slides, we will cover
    what is one of the more confusing parts of Java
    for CS1312 students. If you get lost, just
    remember what weve covered so far
  • 1. When comparing primitives (int, float,
    etc.), use
  • 2. When comparing objects (String, or any class
    you create), use the equals() method.
  • 3. When you create a class that serves as a
    data type or record, remember to include an
    equals() method.
  • In the slides ahead we will note some rare
    exceptions where the comparison will work
    for objects. (Yes, you can use to compare
    objects, but not in all circumstances!)
  • When in doubt, please just follow the principles
    above.


33
Objects and References Review of the normal case
Remember the general pattern of our previous
example box1 new Box(1, 2, 3) box2 new
Box(8, 5, 7)box1 box2 System.out.println(box1
box2) // prints true //
Does box1 reference the same object that box2
references? System.out.println(box1.equals(box2))
// prints true // Does box1
reference an object that has the same contents//
as the object referenced by box2?
box1
box2
L1, W2, H3
memory
L8, W5, H7
34
Equality with References to Objects Strings are
special
As part of the Java Language Specification (the
rules for the Java Language), Strings have a
special characteristic. It turns out that in
some circumstance (BUT NOT ALL), you can use
to compare Strings, in addition to .equals().
Consider
String strHello1 Hello String strHello2
Hello
We would expect these lines to produce the
following memory changes
Hello
strHello1 strHello2
Hello
In fact, it produces the following results in
memory
strHello1 strHello2
Hello
35
Huh? Why are Strings treated differently?
Strings are sometimes a special case of
equivalency in Java. When the compiler
encounters the lines String strHello1
Hello String strHello2 Hello the
compiler is smart enough to know that the two
Strings are identical. So, it decides it will
save a few bytes of memory and point to the same
location in memory. The same result would occur
even if you wrote String strHello2 Hell
o This means that for the above lines of
code, equals() and both work System.out.pri
ntln (strHello1.equals(strHello2)) //
true System.out.println (strHello1
strHello2) // also true, but

// dangerous . . .
36
Exception to the exception with String
But this special case for comparison of
Strings DOES NOT ALWAYS WORK . . . Consider If
one of the Strings were created with use of the
new keyword, the two Strings would no longer
share memory. (That is, would be false, but
equals() would still be true, if the contents of
the Strings are the same.) So, theres an
exception to the exception for Strings when you
dont use the String exception to object
instantiation. Confusing? Exceptionally
so! LESSON DONT USE THE EXCEPTION. Dont
compare Strings, or any Object, with , even
if you think the exception will
apply. REMEMBER Just compare primitives with
, Objects, including Strings, with equals()
-- and it always works fine!
37
toString()or not to String...
Debugging
38
Debugging Strategies
  • Incremental Programming
  • The Idea Find and repair bugs in the small
    before you have a program with several
    components. The hardest thing is finding the
    errors. So, find them as you create each class.
  • Thus, do not
  • write your entire program, then
  • type in your entire program, then
  • attempt to test your entire program
  • Instead
  • design your program at high level, then
  • focus on one class at a time,
  • for each class, write and test before going
    on to the next one.
  • This way, you deal with bugs when its easiest!

39
Debugging Strategies
  • Potential problems with state objects
  • State incorrectly modified by modifer methods.
  • State incorrectly represented by accessor
    methods.
  • Need
  • A way to see the state of the object.
  • Means
  • public String toString ( )
  • Create one toString method per class.
  • Now we can use a reference to an object directly
    as an argument in
  • System.out.println ( )

40
Debugging Strategies
Example of using toString If we have a method
for Class Box public String toString ( )
String returnStr returnStr new
String(Box length iLength
, Width iWidth height
iHeight) return (returnStr) // of
toString Then we can do Box subwooferBox
new Box(40, 50, 60)
System.out.println ( subwooferBox ) //
what???????? // wait a minute where was the
call to toString????
41
Special invocation of toString method
  • Using an object reference when a String is needed
    will implicitly invoke the objects toString
    method. Bonus!
  • This is why it is critical that you actually
    provide a meaningful toString method for
    practically every class you create.
  • Excellent for debugging!

42
Debugging Strategies
One main per Class
  • According to Java, you only need one main per
    program. . . not one per class.
  • But Java doesnt know how to program!
  • To test/debug a class, create a main method for
    the class as part of the class . . .
  • Include in test mains the
  • declaration of variables
  • the invocation of methods
  • the generation of output (e.g. using toString())
  • that will allow you to see what the class
    actually does!
  • Then, invoke the main as part of your testing
    process.
  • The main can stay there as part of your class .
    . . invoked only when you call it for purpose
    of testing.

43
What?
  • Suppose you have class A, class B, and class C
    each has a main method for debugging purposes.
  • Class C has the real main for your overall
    program, and uses classes A B.
  • How can you invoke a particular main?
  • javac A.java lt- compile class A
  • java A lt- invokes As main for
    testing A
  • javac B.java lt- compile class B
  • java B lt- invokes Bs main for
    testing B
  • javac C.java lt compile class C
  • java C lt- invokes Cs main

44
Debugging Strategies
  • Define a boolean constant in every class called
    DEBUG
  • public static final boolean DEBUG true
  • Wherever appropriate insert...
  • if(DEBUG)
  • System.out.println("methodgtvar "var)
  • Customize as necessary!

45
Questions?
46
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com