Lecture 3: Fields, Methods, & Constructors

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
CSC 212 – Data Structures Lecture 12: Java Review.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Problem of the Day  Why are manhole covers round?
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Topics Instance variables, set and get methods Encapsulation
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Problem of the Day  Why are manhole covers round?
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Information and Computer Sciences University of Hawaii, Manoa
Values vs. References Lecture 13.
User-Written Functions
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Static data members Constructors and Destructors
Lecture 2: Primitives & References
CSE 116/504 – Intro. To Computer Science for Majors II
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Lecture 11 C Parameters Richard Gesick.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Variables ICS2O.
6 Chapter Functions.
Classes, Encapsulation, Methods and Constructors (Continued)
Implementing Non-Static Features
CHAPTER 6 GENERAL-PURPOSE METHODS
Recap Week 2 and 3.
Building Java Programs
Object Oriented Programming in java
COP 3330 Object-oriented Programming in C++
Java Programming Language
References Revisted (Ch 5)
Classes and Objects Object Creation
Corresponds with Chapter 5
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Lecture 3: Fields, Methods, & Constructors CSE 116/504 – Intro. To Computer Science for Majors II Lecture 3: Fields, Methods, & Constructors

Announcements 130 students (64%) joined Piazza as of 9AM Important since used for important announcements Sign up today to avoid problems later on

SIGN UP TODAY Announcements 130 students (64%) joined Piazza as of 9AM Important since used for important announcements Sign up today to avoid problems later on

Today’s Goals Experiments with TopHat before grading begins Portion of course grade & that starts counting on Wed. Review object workings & use in more detail Remember how methods called & what this does return statement reviewed & its importance in code Passing arguments to method & what parameter does Use of fields within method & what effects would be

Ice Cream Flavor(s) You Like Chocolate Vanilla Peanut Butter Chunk Banana Sardine and Sauerkraut

Classes vs. Objects Classes are blueprints describing data type Classes (usually) cannot do anything on their own Objects are instances of a class New objects created (instantiated) using new Fields describe state of an object Object’s behavior represented by methods

Instance Variables All of class's instances have same fields… … but values can differ between each instance In a class, each field must have unique name Different classes can duplicate names of fields Field declaration must include data type Primitive or reference type can be used; either legal Field ends up behaving like other primitive or reference

Class Example public class Car { /** What kind of car & who made it */ private String makeAndModel; /** Color of the car. */ private String color; /** Percent full the gas tank is */ private float tankLevel; /** Miles recorded on the odometer */ private int odometerReading; /* Definition continues from here */

Using Fields (1) Car profHertzCar = new Car(); profHertzCar.makeAndModel = “BMW Z4”; profHertzCar.color = “Deep Green Metallic”; profHertzCar.tankLevel = 1.0; profHertzCar.odometerReading = 10000; Car actualCar = new Car(); actualCar.makeAndModel = “Subaru Outback"; actualCar.color = “Brown”; actualCar.tankLevel = 0.0001; actualCar.odometerReading = 104634;

Each instance gets own copy of instance variables Fields Key Concept #1 Each instance gets own copy of instance variables

Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000;

Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000; Car realCar = dreamCar; realCar.makeAndModel = “Subaru Outback”; realCar.color = “Silver”; realCar.tankLevel = 0.0001; realCar.odometerReading = 67634;

Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000; Car realCar = dreamCar; realCar.makeAndModel = “Subaru Outback”; realCar.color = “Silver”; realCar.tankLevel = 0.0001; realCar.odometerReading = 104634;

Each instance gets own copy of instance variables Fields Key Concept #1++ Each instance gets own copy of instance variables

Aliased variables see ALL changes to instance variables Fields Key Concept #2 Aliased variables see ALL changes to instance variables

Using Fields (3) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”;

Using Fields (3) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; Car realCar = new Car(); realCar.makeAndModel = “Subaru Outback”; realCar.color = dreamCar.color;

Using Fields (3) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; Car realCar = new Car(); realCar.makeAndModel = “Subaru Outback”; realCar.color = dreamCar.color; realCar.odometerReading = 104634; realCar.tankLevel = dreamCar.tankLevel;

Fields Key Concept #3 Fields initialized to default value of 0, false, or null (almost all other variables need assignment before use)

Tracing With Fields Each instance gets own copy of instance variables Show in instance’s box, otherwise like other variables Primitive values in instance & get copied on assignment Assignments make alias; references shown as arrow

Tracing With Fields Each instance gets own copy of instance variables Show in instance’s box, otherwise like other variables Primitive values in instance & get copied on assignment Assignments make alias; references shown as arrow

Tracing With Fields public class Player { private int num; private Player backup; public static void main(String[] args) { Player player1, player2, player3; player1 = new Player(); player1.num = 33; player2 = new Player(); player2.num = 42; player2.backup = player1; player3 = player1; player3.num = 68; }

Instance Variables Review Each instance gets own copy of every field Must specify instance whose field should be used Within code, instance can update value at any time No limit on use in code, but need the instance As with any variable, primitive or reference okay Declare anywhere outside of method (but in class): type name;

Methods Define how objects act, behave, & change Every method in class must have unique signature Method name or parameter's order&type must differ Important for code: makes obvious which to execute Return type also need to be declared by methods Cannot be only difference: type not part of signature Caller can skip using result, so does not help find method

Return Type Methods must specify their return type Primitive or reference type if they will return 1 value double squareIt(float x); ArrayList<String> primes(int n); Professor getFavoriteTeacher(); Special return type, void, used if no value is returned void goodSoldier(); void doSomethingWithX(int x);

return Examples void moveAlong() { System.out.println("Nothing to see here"); return null; } float squared(int val) { double retVal = val * (double)val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Examples void moveAlong() { System.out.println("Nothing to see here"); return null; } float squared(int val) { double retVal = val * (double)val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Examples void moveAlong() { System.out.println("Nothing to see here"); } float squared(int val) { double retVal = val * (double)val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Examples void moveAlong() { System.out.println("Nothing to see here"); } float squared(int val) { double retVal = val * (double)val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Examples void moveAlong() { System.out.println("Nothing to see here"); } float squared(int val) { double retVal = val * (double)val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Examples void moveAlong() { System.out.println("Nothing to see here"); } float squared(int val) { long retVal = val * val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Examples void moveAlong() { System.out.println("Nothing to see here"); } float squared(int val) { long retVal = val * val; return retVal; } String getRandomLetter() { java.util.Random rand = new Random(); char ch = (char)rand.nextInt(); return "\'" + ch + "\'"; ch = Character.toLowerCase(ch); }

return Statement Function ends when return gets executed Value in return statement is result from method Calling function resumes execution from the method call If executable code is after return, Java will not compile

return Statement Key Concept

Return Value If value returned, either a primitive or reference Call "evaluates" to return value when line gets executed Debugging & reuse value by setting variable to result Object aliased in assignment, if return value reference If return value is primitive, result copied into variable Nothing returned by void method, no result to use Remember: will get error trying to return from void Error using result of void method call, by extension

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Calling Key Concept #1

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Calling Key Concept #2 Calls cannot be on left-hand side of assignment Ignore what we are using in the return statement. It is the VALUE of a field or variable that is returned, not the variable itself

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Calling Key Concept #3 Can use result of method call just like any value (but this is bad style, so don't)

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Method Call Examples void drive() { /* Code skipped for space */ } float checkGas(double added) { /* Code skipped for space */ } Car getDreamCar() { /* Code skipped for space */ } int distanceMoved = drive(); drive(); Object obj = drive(); if (checkGas(0.2) < checkGas(-0.1)) System.out.println("I think we have a problem"); checkGas(fillUp) = 1.0; Car tesla = getDreamCar(); if (getDreamCar().odometer > 100000) { System.out.println("I'll wait"); getDreamCar() = null; }

Local Variables Inside method, local variables can be declared No defaults exist: must assign value before use "Die" when block completes; value not saved if run again Always declare local in smallest enclosing block Reduces chance of error & simplifies reading of code No time needed to create so program will be just as fast Compiler does this anyway, so no savings from skipping As an added bonus, debugging will be much simpler

Local Variable Key Concept Add locals whenever they will make code readable (Compiler does this anyway!)

Methods' Parameters Each method can also declare parameters Leave blank if no parameters wanted in method Like variables, parameters declared with name & type Parameters behave like variables scoped to method void bladder() { // Code skipped for space } short cash(byte loanShark) { // Code skipped } long johnSilver(Rum drinking, float boat) { // Code goes here, skipped for space }

Are Parameters Variables? At start of method, assigned value of argument Works like normal assignment for the parameters type This means that primitives copied & references aliased Parameters "live" only during call & die once over Parameter placed in memory at top of frame on stack Variables also found in frame just at lower addresses

Are Parameters Variables? At start of method, assigned value of argument Works like normal assignment for the parameters type This means that primitives copied & references aliased Parameters "live" only during call & die once over Parameter placed in memory at top of frame on stack Variables also found in frame just at lower addresses

Parameter like local which copies/aliases value passed in Parameter Key Concept Parameter like local which copies/aliases value passed in

Best Choice of Variable Does code calculate & use value inside single call? Use local variable & avoid bugs from using old values Value created outside, but used inside single call? Use parameter: value passed in, but limits reuse errors Does instance need value after method call ends? Use instance variable so that value remains available

Constructors Special methods called to instantiate object Identical name as class; can have o or more parameters If signatures differ, multiple constructors possible No return type (not even void); issues occur if added Arguments to new must match class constructor When no constructor defined, implicit constructor used No parameters included in implicit constructor

Constructors Key Concept A method with same name as class BUT NO RETURN TYPE

Every instance gets own copy of instance variables Fields Key Concept #1 Every instance gets own copy of instance variables

Aliased variables see ALL changes to instance variables Fields Key Concept #2 Aliased variables see ALL changes to instance variables

Fields Key Concept #3 Fields initialized to default value of 0, false, or null (almost all other variables need assignment before use)

return Statement Key Concept

Method Calling Key Concept #1

Method Calling Key Concept #2 Calls cannot be on left-hand side of assignment Ignore what we are using in the return statement. It is the VALUE of a field or variable that is returned, not the variable itself

Method Calling Key Concept #3 Can use result of method call just like any value (but this is bad style, so don't)

Parameter like local which copies/aliases value passed in Parameter Key Concept Parameter like local which copies/aliases value passed in

Constructors Key Concept A method with same name as class BUT NO RETURN TYPE

For Next Lecture Keep reviewing your Java lessons Should be getting back up-to-speed with Java basics Friday will discuss how fields in an object work How to store values between calls to a method? There are weekly assignment problems to do Week #1 due next Thursday in week one special case Should help finish removing rust from lazy summer Enjoy long weekend; last break for several months Definitely needed after long, hard days of work