Computer Science I Classes and Objects Professor: Evan Korth New York University Evan Korth New York University.
Download ReportTranscript Computer Science I Classes and Objects Professor: Evan Korth New York University Evan Korth New York University.
Computer Science I Classes and Objects Professor: Evan Korth New York University Evan Korth New York University Road Map • Class modifiers • Garbage collection • Naming conflicts – this • Reference members • Reading: – Liang 7: chapter 7: 7.9, 7.10, 9.3, 9.4 – Liang 8: chapter 8: 8.6 – 8.9 Evan Korth New York University review • • • • What does encapsulation mean? What is a data member? What is a method member? What is the difference between an object and a class? • What does the following line of code do? – Integer i; • What is i above? • What happens if you make a class without a constructor? Evan Korth New York University Review (cont) • What do the following modifiers mean when applied to a data member? – final – static – public – private • What if there is no modifier? • What is the principle of least privilege? Evan Korth New York University Review (cont) • What data type does a set method usually return? • What parameter does a get method usually take? • A class has 3 objects instantiated, it also has a static variable called x and an instance variable called y. – How many x values are stored in memory? – How many y values are stored in memory? • What is the scope of an instance variable? • Can you call an instance method without an instance of the class? Evan Korth New York University Class modifiers • No modifier (default) means the class is visible in the package in which it is declared. • public means it is visible to everything. • There are two others (final and abstract) which we will discuss later in the semester. Evan Korth New York University Garbage collection • When an object is no longer referenced by any reference variable, that object is referred to as garbage. • Java automatically tracks garbage objects and frees its memory when the garbage collector runs. • We do not have direct control over when the garbage is collected. • We can suggest to the compiler to collect garbage but it is not guaranteed that it will run. • To suggest garbage collection we make the following method call: – System.gc(); Evan Korth New York University Anonymous objects • An object without a reference is called an anonymous object. • It is created, used and immediately marked as garbage. Evan Korth New York University Variable name conflicts • It is possible to have a variable name in a method with the same name as a data member in a class. • In such case, the local method variable “hides” the data member variable. Evan Korth New York University Keyword this • The keyword this is used within a class to refer to the specific instance of the class that is being used. • A variable in a class’ method that has the same name as a field will “shadow” the field. You can access the field using the this keyword. • You cannot use the this keyword in static methods. (why?) Evan Korth New York University Another use for this • this (args) in a constructor will invoke another constructor of that class. – If you call another constructor from a constructor, it must be the first line in the calling constructor. • This is useful when you overload your constructors. In general, a constructor with fewer parameters should call a constructor with more parameters. Evan Korth New York University Composition • The term composition refers to the practice of having an object as a data member within another object. • What is actually stored is a reference to the member object. (therefore we can have self referential objects) • The default value for a reference variable is null. Evan Korth New York University Passing reference variables to methods • All variables in Java are passed using call by value. However, since object variables are really references to objects, passing an object is simulated pass by reference. – Objects passed to a method and modified by that method will have the changes reflected in the calling method. – Primitive variables passed to a method and modified by that method will NOT have the changes reflected in the calling method. Evan Korth New York University