1 / 13

Computer Science I Classes and Objects Professor: Evan Korth New York University

Computer Science I Classes and Objects Professor: Evan Korth New York University. Road Map. Class modifiers Garbage collection Naming conflicts this Reference members Reading: Liang 5: chapter 6: 6.9, 6.12 Liang 6: chapter 7: 7.10, 7.12, 7.13 Liang 7: chapter 7: 7.9, 7.10, 9.3, 9.4.

lkendra
Download Presentation

Computer Science I Classes and Objects Professor: Evan Korth New York University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science IClasses and ObjectsProfessor: Evan KorthNew York University Evan Korth New York University

  2. Road Map • Class modifiers • Garbage collection • Naming conflicts • this • Reference members • Reading: • Liang 5: chapter 6: 6.9, 6.12 • Liang 6: chapter 7: 7.10, 7.12, 7.13 • Liang 7: chapter 7: 7.9, 7.10, 9.3, 9.4 Evan Korth New York University

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

More Related