{small lecturenumber - heblocknumber :} Java Programsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Java Programsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Classesaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Classesaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Implementation I: Localsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Implementation I: localsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Implementation I: localsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Pass-By-Valueaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Objectsaddtocounter {blocknumber}{1}Intro to Computer Science IICS112-2012S-02Creating ClassesDavid GallesDepartment of Computer ScienceUniversity of San Francisco02-0: Java ProgramsJava programs are a collection of classesEach class is a Template, not an ObjectCan’t use a class until we create an instance(call “new”)One exception: Static MethodsIn this course, we will only use one static method:main method02-1: Java ProgramsSimpliest Java Program:Hello World 3 ways02-2: ClassesClasses contain data and methods (like functionsin python)Methods can access data members (calledinstance variables) of a classSpecial method, called “Constructor”, which iscalled when an object of the class is created usingnew.02-3: ClassesSecond Example: Employee02-4: Implementation I: LocalsVariables declared inside functions are called localvariablesStored on the Call StackCreated when function is calledDissappear when function ends02-5: Implementation I: localsWhen a method is called:Allocate space on the call stack to store methodparameters & variables local to the methodActivation record for the methodCopy values into the space allocated for theparametersExecute the body of the methodPop activation record off the stack02-6: Implementation I: localsExample: MethodTestCodeMemory contents (on whiteboard)02-7: Pass-By-ValueMethods in Java are “Pass-By-Value”Value is copied into the parameterChanges to the parameter don’t change the valuepassed intExample: MethodTest202-8: ObjectsStack is not the only place where data is storedObjects (specifically, instance variables) are storedon the HeapDifferent section of memory than the stackMemory is allocated (or set aside) on the heapthrough a call to new02-9: ObjectsHeapData, HeapDriver102-10: ObjectsEvery object in your program is a pointer tomemory on the heapWhen we pass an object as a parameter, copy thepointerChanges to the pointer itsef are not reflected backto the calling methodChanges made to what the pointer points to arereflected back to the calling method02-11: ObjectsHeapData, HeapDriver2,
View Full Document