SlideShare a Scribd company logo
Lecture 4
Classes and Objects
Review
Solutions 1
public static int getMinIndex(int[] values) {
int minValue = Integer.MAX_VALUE;
int minIndex = -1;
for(int i=0; i<values.length; i++)
if (values[i] < minValue) {
minValue = values[i];
minIndex = i;
}
return minIndex;
}
Solutions 2
public static int getSecondMinIndex(int[] values) {
int secondIdx = -1;
int minIdx= getMinIndex(values);
for(int i=0; i<values.length; i++) {
if (i == minIdx)
continue;
if (secondIdx == -1 ||
values[i] < values[secondIdx])
secondIdx = i;
}
return secondIdx;
}
• What happens if values = {0}? values = {0, 0}? values = {0,1}?
Popular Issues 1
• Array Index vs Array Value
int[] values = {99, 100, 101};
System.out.println(values[0] ); // 99
Values 99 100 101
Indexes 0 1 2
Popular Issues 2
• Curly braces { … } after if/else, for/while
for (int i = 0; i < 5; i++)
System.out.println(“Hi”);
System.out.println(“Bye”);
• What does this print?
Popular Issues 3
• Variable initialization
int getMinValue(int[] vals) {
int min = 0;
for (int i = 0; i < vals.length; i++) {
if (vals[i] < min) {
min = vals[i]
}
}
}
Problem?
• What if vals = {1,2,3}?
• Set min = Integer.MAX_VALUE or vals[0]
Popular Issues 4
• Variable Initialization – secondMinIndex
int minIdx = getMin(vals)
int secondIdx = 0;
for (int i = 0; i < vals.length; i++) {
if (i == minIdx) continue;
if (vals[i] < vals[secondIdx])
secondIdx = i;
}
• What if vals = {0, 1, 2}?
• See solutions
Popular Issues 5
Defining a method inside a method
public static void main(String[] arguments) {
public static void foobar () {
}
}
Debugging Notes 1
• Use System.out.println throughout your code
to see what it’s doing
for ( int i=0; i< vals.length; i++) {
if ( vals[i] < minVal) {
System.out.println(“cur min: ” + minVal);
System.out.println(“new min: ” + vals[i]);
minVal = vals[i];
}
}
Debugging Notes 2
• Formatting
• Ctrl-shift-f is your friend
for (int i = 0; i < vals.length; i++) {
if (vals[i] < vals[minIdx]) {
minIdx=i;}
return minIdx;}
• Is there a bug? Who knows! Hard to read
Today’s Topics
Object oriented programming
Defining Classes
Using Classes
References vs Values
Static types and methods
Today’s Topics
Object oriented programming
Defining Classes
Using Classes
References vs Values
Static types and methods
Object oriented programming
• Represent the real world
Baby
Object oriented programming
• Represent the real world
Baby
Name
Sex
Weight
Decibels
# poops so far
Object Oriented Programming
• Objects group together
– Primitives (int, double, char, etc..)
– Objects (String, etc…)
Baby
String name
boolean isMale
double weight
double decibels
int numPoops
Why use classes?
• Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
Why use classes?
• Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
// little baby david
String nameDavid2;
double weightDavid2;
David2?
Terrible •
Why use classes?
• Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
// little baby david
String nameDavid2;
double weightDavid2;
David2?
Terrible • 
500 Babies? That Sucks!
Why use classes?
Name
Weight
Sex
…
Baby1
Why use classes?
Name
Weight
Sex
…
496
more
Babies
Name
Weight
Sex
…
Name
Weight
Sex
…
Name
Weight
Sex
…
Baby1 Baby2 Baby3 Baby4 …
Why use classes?
Baby1 Baby2 Baby3 Baby4
496 more
Babies …
Nursery
Why use classes?
Baby1 Baby2 Baby3 Baby4
496 more
Babies …
Nurse1 Nurse2 Nurse3 Nurse4
More nurses…
Nursery
Why use classes?
Baby
Nurse
[]
[]
Nursery
Why use classes?
Nurse
[] ER
[]
Baby
Nursery
Hospital
Defining classes
Class - overview
public class Baby {
String name;
boolean isMale;
double weight;
double decibels;
int numPoops = 0; Class
Definition
void poop() {
numPoops += 1;
System.out.println(“Dear mother, ”+
“I have pooped. Ready the diaper.”);
}
}
Class - overview
Class
Baby myBaby = new Baby();
Instance
Let’s declare a baby!
public class Baby {
}
Let’s declare a baby!
public class Baby {
fields
methods
}
Note
• Class names are Capitalized
• 1 Class = 1 file
• Having a main method means the class can
be run
Baby fields
public class Baby {
TYPE var_name;
TYPE var_name = some_value;
}
Baby fields
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
}
Baby Siblings?
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
XXXXX YYYYY;
}
Baby Siblings?
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
Baby[] siblings;
}
Ok, let’s make this baby!
Baby ourBaby = new Baby();
But what about it’s name? it’s sex?
Constructors
public class CLASSNAME{
CLASSNAME ( ) {
}
CLASSNAME ([ARGUMENTS]) {
}
}
CLASSNAME obj1 = new CLASSNAME();
CLASSNAME obj2 = new CLASSNAME([ARGUMENTS])
Constructors
• Constructor name == the class name
• No return type – never returns anything
• Usually initialize fields
• All classes need at least one constructor
– If you don’t write one, defaults to
CLASSNAME () {
}
Baby constructor
public class Baby {
String name;
boolean isMale;
Baby(String myname, boolean maleBaby){
name = myname;
isMale = maleBaby;
}
}
Baby methods
public class Baby {
String name = “Slim Shady”;
...
void sayHi() {
System.out.println(
“Hi, my name is.. “ + name);
}
}
Baby methods
public class Baby {
String weight = 5.0;
void eat(double foodWeight) {
if (foodWeight >= 0 &&
foodWeight < weight) {
weight = weight + foodWeight;
}
}
}
Baby class
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
Baby[] siblings;
void sayHi() {…}
void eat(double foodWeight) {…}
}
Using classes
Classes and Instances
// class Definition
public class Baby {…}
// class Instances
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”, true);
Baby knox = new Baby(“Knox Jolie-Pitt”, true);
Accessing fields
• Object.FIELDNAME
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”,
true)
System.out.println(shiloh.name);
System.out.println(shiloh.numPoops);
Calling Methods
• Object.METHODNAME([ARGUMENTS])
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”,
true)
shiloh.sayHi() ; // “Hi, my name is ...”
shiloh.eat(1);
References vs Values
Primitives vs References
• Primitive types are basic java types
– int, long, double, boolean, char, short, byte, float
– The actual values are stored in the variable
• Reference types are arrays and objects
– String, int[], Baby, …
How java stores primitives
• Variables are like fixed size cups
• Primitives are small enough that they just fit
into the cup
int double char boolean
How java stores objects
• Objects are too big to fit in a variable
– Stored somewhere else
– Variable stores a number that locates the object
Object
How java stores objects
• Objects are too big to fit in a variable
– Stored somewhere else
– Variable stores a number that locates the object
Object Object Object
Object Object Object
Object’s
location
References
• The object’s location is called a reference
• == compares the references
Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Does shiloh1 == shiloh2?
References
• The object’s location is called a reference
• == compares the references
Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Does shiloh1 == shiloh2?
no
References
Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Name=“shiloh”
Name=“shiloh”
reference reference
shiloh1 shiloh2
References
Baby mybaby = new Baby(“davy”, true)
mybaby.name = “david”
mybaby’s
location
name = ‘davy’
ismale = true
…
References
Baby mybaby = new Baby(‘davy’, true)
mybaby.name = ‘david’
mybaby’s
location
name = ‘david’
Ismale = true
…
References
• Using = updates the reference.
baby1 = baby2
baby2
location
baby1
object
baby2
object
baby2
location
baby1 baby2
References
• Using = updates the reference.
baby1 = baby2
baby2
location
baby1
object
baby2
object
baby1 baby2
References
• using [ ] or
– Follows the reference to the object
– May modify the object, but never the reference
• Imagine
– Following directions to a house
– Moving the furniture around
• Analogous to
– Following the reference to an object
– Changing fields in the object
Methods and references
void doSomething(int x, int[] ys, Baby b) {
x = 99;
ys[0] = 99;
b.name = “99”;
}
...
int i = 0;
int[] j = {0};
Baby k = new Baby(“50”, true);
doSomething(i, j, k);
i=? j=? k=?
static types and methods
static
• Applies to fields and methods
• Means the field/method
– Is defined for the class declaration,
– Is not unique for each instance
static
public class Baby {
static int numBabiesMade = 0;
}
Baby.numBabiesMade = 100;
Baby b1 = new Baby();
Baby b2 = new Baby();
Baby.numBabiesMade = 2;
What is
b1.numBabiesMade?
b2.numBabiesMade?
static example
• Keep track of the number of babies that have
been made.
public class Baby {
int numBabiesMade = 0;
Baby() {
numBabiesMade += 1;
}
}
static field
• Keep track of the number of babies that have
been made.
public class Baby {
static int numBabiesMade = 0;
Baby() {
numBabiesMade += 1;
}
}
static method
public class Baby {
static void cry(Baby thebaby) {
System.out.println(thebaby.name + “cries”);
}
}
Or
public class Baby {
void cry() {
System.out.println(name + “cries”);
}
}
static notes
• Non-static methods can reference static
methods, but not the other way around
– Why?
public class Baby {
String name = “DMX”;
static void whoami() {
System.out.println(name);
}
}
main
• Why is main static?
public static void main(String[] arguments) {
}
Assignment 4
• Modeling Book and Libraries
– class Book {}
– class Library{}
• Books can be
– Borrowed
– Returned
• Library
– Keeps track of books
– Hint: use Book[]
MIT OpenCourseWare
http://ocw.mit.edu
6.092 Introduction to Programming in Java
January (IAP) 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

More Related Content

PPT
Object concepts
PPT
Object concepts
PPTX
Python 2. classes- cruciql for students objects1.pptx
PPTX
L03 Software Design
PPT
Object-oriented concepts
PDF
python note.pdf
PPTX
Java tutorial part 3
Object concepts
Object concepts
Python 2. classes- cruciql for students objects1.pptx
L03 Software Design
Object-oriented concepts
python note.pdf
Java tutorial part 3

Similar to LECTURE 4 CLASSES AND OBJECTS.pdf (20)

PDF
classes and objects.pdf
PDF
The Magical Art of Extracting Meaning From Data
PPTX
Front end fundamentals session 1: javascript core
PPTX
Classes_and_Objects_in_Pythonoopsconcept.pptx
PPT
cs1311lecture22wdl.ppt this is oops feature polymorphism
PPTX
PPT
cs1311lecture22wdl.ppt
PPT
cs1311lecture22wdl (1).ppt
PPT
Pplymorphism
PPT
Oops Concept Java
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
PPTX
Creating Objects in Python
PPTX
Objects and Classes in JAVA introduction
PPT
M18 learning
PPTX
Math for everyone thesis presentation
PDF
Lecture 1 - Objects and classes
PDF
Introduction to web programming for java and c# programmers by @drpicox
PPT
Introduction to Java(basic understanding).ppt
classes and objects.pdf
The Magical Art of Extracting Meaning From Data
Front end fundamentals session 1: javascript core
Classes_and_Objects_in_Pythonoopsconcept.pptx
cs1311lecture22wdl.ppt this is oops feature polymorphism
cs1311lecture22wdl.ppt
cs1311lecture22wdl (1).ppt
Pplymorphism
Oops Concept Java
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
Creating Objects in Python
Objects and Classes in JAVA introduction
M18 learning
Math for everyone thesis presentation
Lecture 1 - Objects and classes
Introduction to web programming for java and c# programmers by @drpicox
Introduction to Java(basic understanding).ppt
Ad

Recently uploaded (20)

PDF
Dating-Courtship-Marriage-and-Responsible-Parenthood.pdf
PDF
Chapter 3 about The site of the first mass
PDF
630895715-Romanesque-Architecture-ppt.pdf
PPTX
CPAR-ELEMENTS AND PRINCIPLE OF ARTS.pptx
PPTX
SlideEgg_21518-Company Presentation.pptx
PDF
Arts and Crats of Cagayan and Central Luzon.pdf
PPTX
Green and Orange Illustration Understanding Climate Change Presentation.pptx
PPTX
Visual-Arts.pptx power point elements of art the line, shape, form
PPTX
Socio ch 1 characteristics characteristics
PPTX
Callie Slide Show Slide Show Slide Show S
PPSX
Multiple scenes in a single painting.ppsx
PPTX
The-Evolution-of-Comedy-in-America-A-Conversation-with-Dan-Nainan.pptx
PPTX
Certificados y Diplomas para Educación de Colores Candy by Slidesgo.pptx
PDF
Slide_BIS 2020 v2.pdf....................................
PPTX
Slide_Egg-81850-About Us PowerPoint Template Free.pptx
PPTX
Copy of Executive Design Pitch Deck by Slidesgo.pptx.pptx
PPTX
4277547e-f8e2-414e-8962-bf501ea91259.pptx
PPTX
Theatre Studies - Powerpoint Entertainmn
PPTX
Presentation on tradtional textiles of kutch
PPTX
EJ Wedding 520 It's official! We went to Xinyi District to do the documents
Dating-Courtship-Marriage-and-Responsible-Parenthood.pdf
Chapter 3 about The site of the first mass
630895715-Romanesque-Architecture-ppt.pdf
CPAR-ELEMENTS AND PRINCIPLE OF ARTS.pptx
SlideEgg_21518-Company Presentation.pptx
Arts and Crats of Cagayan and Central Luzon.pdf
Green and Orange Illustration Understanding Climate Change Presentation.pptx
Visual-Arts.pptx power point elements of art the line, shape, form
Socio ch 1 characteristics characteristics
Callie Slide Show Slide Show Slide Show S
Multiple scenes in a single painting.ppsx
The-Evolution-of-Comedy-in-America-A-Conversation-with-Dan-Nainan.pptx
Certificados y Diplomas para Educación de Colores Candy by Slidesgo.pptx
Slide_BIS 2020 v2.pdf....................................
Slide_Egg-81850-About Us PowerPoint Template Free.pptx
Copy of Executive Design Pitch Deck by Slidesgo.pptx.pptx
4277547e-f8e2-414e-8962-bf501ea91259.pptx
Theatre Studies - Powerpoint Entertainmn
Presentation on tradtional textiles of kutch
EJ Wedding 520 It's official! We went to Xinyi District to do the documents
Ad

LECTURE 4 CLASSES AND OBJECTS.pdf

  • 3. Solutions 1 public static int getMinIndex(int[] values) { int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int i=0; i<values.length; i++) if (values[i] < minValue) { minValue = values[i]; minIndex = i; } return minIndex; }
  • 4. Solutions 2 public static int getSecondMinIndex(int[] values) { int secondIdx = -1; int minIdx= getMinIndex(values); for(int i=0; i<values.length; i++) { if (i == minIdx) continue; if (secondIdx == -1 || values[i] < values[secondIdx]) secondIdx = i; } return secondIdx; } • What happens if values = {0}? values = {0, 0}? values = {0,1}?
  • 5. Popular Issues 1 • Array Index vs Array Value int[] values = {99, 100, 101}; System.out.println(values[0] ); // 99 Values 99 100 101 Indexes 0 1 2
  • 6. Popular Issues 2 • Curly braces { … } after if/else, for/while for (int i = 0; i < 5; i++) System.out.println(“Hi”); System.out.println(“Bye”); • What does this print?
  • 7. Popular Issues 3 • Variable initialization int getMinValue(int[] vals) { int min = 0; for (int i = 0; i < vals.length; i++) { if (vals[i] < min) { min = vals[i] } } } Problem? • What if vals = {1,2,3}? • Set min = Integer.MAX_VALUE or vals[0]
  • 8. Popular Issues 4 • Variable Initialization – secondMinIndex int minIdx = getMin(vals) int secondIdx = 0; for (int i = 0; i < vals.length; i++) { if (i == minIdx) continue; if (vals[i] < vals[secondIdx]) secondIdx = i; } • What if vals = {0, 1, 2}? • See solutions
  • 9. Popular Issues 5 Defining a method inside a method public static void main(String[] arguments) { public static void foobar () { } }
  • 10. Debugging Notes 1 • Use System.out.println throughout your code to see what it’s doing for ( int i=0; i< vals.length; i++) { if ( vals[i] < minVal) { System.out.println(“cur min: ” + minVal); System.out.println(“new min: ” + vals[i]); minVal = vals[i]; } }
  • 11. Debugging Notes 2 • Formatting • Ctrl-shift-f is your friend for (int i = 0; i < vals.length; i++) { if (vals[i] < vals[minIdx]) { minIdx=i;} return minIdx;} • Is there a bug? Who knows! Hard to read
  • 12. Today’s Topics Object oriented programming Defining Classes Using Classes References vs Values Static types and methods
  • 13. Today’s Topics Object oriented programming Defining Classes Using Classes References vs Values Static types and methods
  • 14. Object oriented programming • Represent the real world Baby
  • 15. Object oriented programming • Represent the real world Baby Name Sex Weight Decibels # poops so far
  • 16. Object Oriented Programming • Objects group together – Primitives (int, double, char, etc..) – Objects (String, etc…) Baby String name boolean isMale double weight double decibels int numPoops
  • 17. Why use classes? • Why not just primitives? // little baby alex String nameAlex; double weightAlex; // little baby david String nameDavid; double weightDavid;
  • 18. Why use classes? • Why not just primitives? // little baby alex String nameAlex; double weightAlex; // little baby david String nameDavid; double weightDavid; // little baby david String nameDavid2; double weightDavid2; David2? Terrible •
  • 19. Why use classes? • Why not just primitives? // little baby alex String nameAlex; double weightAlex; // little baby david String nameDavid; double weightDavid; // little baby david String nameDavid2; double weightDavid2; David2? Terrible • 500 Babies? That Sucks!
  • 22. Why use classes? Baby1 Baby2 Baby3 Baby4 496 more Babies … Nursery
  • 23. Why use classes? Baby1 Baby2 Baby3 Baby4 496 more Babies … Nurse1 Nurse2 Nurse3 Nurse4 More nurses… Nursery
  • 25. Why use classes? Nurse [] ER [] Baby Nursery Hospital
  • 27. Class - overview public class Baby { String name; boolean isMale; double weight; double decibels; int numPoops = 0; Class Definition void poop() { numPoops += 1; System.out.println(“Dear mother, ”+ “I have pooped. Ready the diaper.”); } }
  • 28. Class - overview Class Baby myBaby = new Baby(); Instance
  • 29. Let’s declare a baby! public class Baby { }
  • 30. Let’s declare a baby! public class Baby { fields methods }
  • 31. Note • Class names are Capitalized • 1 Class = 1 file • Having a main method means the class can be run
  • 32. Baby fields public class Baby { TYPE var_name; TYPE var_name = some_value; }
  • 33. Baby fields public class Baby { String name; double weight = 5.0; boolean isMale; int numPoops = 0; }
  • 34. Baby Siblings? public class Baby { String name; double weight = 5.0; boolean isMale; int numPoops = 0; XXXXX YYYYY; }
  • 35. Baby Siblings? public class Baby { String name; double weight = 5.0; boolean isMale; int numPoops = 0; Baby[] siblings; }
  • 36. Ok, let’s make this baby! Baby ourBaby = new Baby(); But what about it’s name? it’s sex?
  • 37. Constructors public class CLASSNAME{ CLASSNAME ( ) { } CLASSNAME ([ARGUMENTS]) { } } CLASSNAME obj1 = new CLASSNAME(); CLASSNAME obj2 = new CLASSNAME([ARGUMENTS])
  • 38. Constructors • Constructor name == the class name • No return type – never returns anything • Usually initialize fields • All classes need at least one constructor – If you don’t write one, defaults to CLASSNAME () { }
  • 39. Baby constructor public class Baby { String name; boolean isMale; Baby(String myname, boolean maleBaby){ name = myname; isMale = maleBaby; } }
  • 40. Baby methods public class Baby { String name = “Slim Shady”; ... void sayHi() { System.out.println( “Hi, my name is.. “ + name); } }
  • 41. Baby methods public class Baby { String weight = 5.0; void eat(double foodWeight) { if (foodWeight >= 0 && foodWeight < weight) { weight = weight + foodWeight; } } }
  • 42. Baby class public class Baby { String name; double weight = 5.0; boolean isMale; int numPoops = 0; Baby[] siblings; void sayHi() {…} void eat(double foodWeight) {…} }
  • 44. Classes and Instances // class Definition public class Baby {…} // class Instances Baby shiloh = new Baby(“Shiloh Jolie-Pitt”, true); Baby knox = new Baby(“Knox Jolie-Pitt”, true);
  • 45. Accessing fields • Object.FIELDNAME Baby shiloh = new Baby(“Shiloh Jolie-Pitt”, true) System.out.println(shiloh.name); System.out.println(shiloh.numPoops);
  • 46. Calling Methods • Object.METHODNAME([ARGUMENTS]) Baby shiloh = new Baby(“Shiloh Jolie-Pitt”, true) shiloh.sayHi() ; // “Hi, my name is ...” shiloh.eat(1);
  • 48. Primitives vs References • Primitive types are basic java types – int, long, double, boolean, char, short, byte, float – The actual values are stored in the variable • Reference types are arrays and objects – String, int[], Baby, …
  • 49. How java stores primitives • Variables are like fixed size cups • Primitives are small enough that they just fit into the cup int double char boolean
  • 50. How java stores objects • Objects are too big to fit in a variable – Stored somewhere else – Variable stores a number that locates the object Object
  • 51. How java stores objects • Objects are too big to fit in a variable – Stored somewhere else – Variable stores a number that locates the object Object Object Object Object Object Object Object’s location
  • 52. References • The object’s location is called a reference • == compares the references Baby shiloh1 = new Baby(“shiloh”); Baby shiloh2 = new Baby(“shiloh”); Does shiloh1 == shiloh2?
  • 53. References • The object’s location is called a reference • == compares the references Baby shiloh1 = new Baby(“shiloh”); Baby shiloh2 = new Baby(“shiloh”); Does shiloh1 == shiloh2? no
  • 54. References Baby shiloh1 = new Baby(“shiloh”); Baby shiloh2 = new Baby(“shiloh”); Name=“shiloh” Name=“shiloh” reference reference shiloh1 shiloh2
  • 55. References Baby mybaby = new Baby(“davy”, true) mybaby.name = “david” mybaby’s location name = ‘davy’ ismale = true …
  • 56. References Baby mybaby = new Baby(‘davy’, true) mybaby.name = ‘david’ mybaby’s location name = ‘david’ Ismale = true …
  • 57. References • Using = updates the reference. baby1 = baby2 baby2 location baby1 object baby2 object baby2 location baby1 baby2
  • 58. References • Using = updates the reference. baby1 = baby2 baby2 location baby1 object baby2 object baby1 baby2
  • 59. References • using [ ] or – Follows the reference to the object – May modify the object, but never the reference • Imagine – Following directions to a house – Moving the furniture around • Analogous to – Following the reference to an object – Changing fields in the object
  • 60. Methods and references void doSomething(int x, int[] ys, Baby b) { x = 99; ys[0] = 99; b.name = “99”; } ... int i = 0; int[] j = {0}; Baby k = new Baby(“50”, true); doSomething(i, j, k); i=? j=? k=?
  • 61. static types and methods
  • 62. static • Applies to fields and methods • Means the field/method – Is defined for the class declaration, – Is not unique for each instance
  • 63. static public class Baby { static int numBabiesMade = 0; } Baby.numBabiesMade = 100; Baby b1 = new Baby(); Baby b2 = new Baby(); Baby.numBabiesMade = 2; What is b1.numBabiesMade? b2.numBabiesMade?
  • 64. static example • Keep track of the number of babies that have been made. public class Baby { int numBabiesMade = 0; Baby() { numBabiesMade += 1; } }
  • 65. static field • Keep track of the number of babies that have been made. public class Baby { static int numBabiesMade = 0; Baby() { numBabiesMade += 1; } }
  • 66. static method public class Baby { static void cry(Baby thebaby) { System.out.println(thebaby.name + “cries”); } } Or public class Baby { void cry() { System.out.println(name + “cries”); } }
  • 67. static notes • Non-static methods can reference static methods, but not the other way around – Why? public class Baby { String name = “DMX”; static void whoami() { System.out.println(name); } }
  • 68. main • Why is main static? public static void main(String[] arguments) { }
  • 69. Assignment 4 • Modeling Book and Libraries – class Book {} – class Library{} • Books can be – Borrowed – Returned • Library – Keeps track of books – Hint: use Book[]
  • 70. MIT OpenCourseWare http://ocw.mit.edu 6.092 Introduction to Programming in Java January (IAP) 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.