Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
DATA STRUCTURES Lecture: Initialization & Cleanup Slides adapted from Prof. Steven Roehrig.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Java Software Solutions
AAA. Today’s lecture Review of Chapter 6 Go over examples.
Road Map Introduction to object oriented programming. Classes
Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Languages: Java vs C++
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
CSC Programming I Lecture 8 September 9, 2002.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Session 7 Methods Strings Constructors this Inheritance.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Core Java Garbage Collection LEVEL – PRACTITIONER.
Topic: Java Garbage Collection
Static data members Constructors and Destructors
Unit-2 Objects and Classes
University of Central Florida COP 3330 Object Oriented Programming
Java Review: Reference Types
This pointer, Dynamic memory allocation, Constructors and Destructor
Constructor Overloading
Finalization 17: Finalization
Object Oriented Programming in java
CLEANING UP UNUSED OBJECTS
Assessment – Java Basics: Part 1
2.1 Introduction to Object-Oriented Programming
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
String Class.
SPL – PS3 C++ Classes.
Presentation transcript:

Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001

Using the this reference The this keyword refers to the current class. You can use the this keyword to refer to a data member or method of the current class.

Static keyword The static keyword is used as modifiers for data members, member functions. The static keyword is also used for inner classes. The static method cannot access the non-static data members. The static data members can be initialized with a static method. For example : static { class data member=initialized value }

Finalizers Java does not provide destructors. Instead it gives you the option of using the finalize method to perform the cleanup process. The finalize method should have the protected keyword. The garbage collector optionally calls the finalize method before it disposed the object.

Garbage Collectors Automated memory management is one of Java's most valuable services. Many languages require programmers to specify the details of acquiring storage from the operating system and returning it when no longer needed. But, Java handles this for programmer. Since Java does automatic garbage collection, manual memory reclamation is not needed. The runtime system tracks each object you create, notices when the last reference to it has vanished, and frees the object for you. Of course, some objects utilize a resource other than memory, such as a file or a handle to another object that uses system resources. In this case it is important that the resource be reclaimed and recycled when it is no longer needed.

Alternatively, you can call the method System.gc() -to run garbage collector at any point you choose.(doesn't mean you will get it). The finalize() method will be called before the garbage collector sweeps away the object. In practice, do not rely on the finalize method for recycling any resources that are in short supply - you simply cannot know when this method will be called. If a finalize() method exist for an object it is guaranteed to be called once (& only once) for the object before the object is garbage collected.

public void method() { BigObject bo = new BigObject(2000); //do something //let the runtime environment know that "bo" is no longer needed bo = null; } The runtime environment could collect the "BigObject" anytime after "bo" is set to null. Setting the reference to null doesn't guarantee that the object will be garbage collected quickly, but it does help.

You must be able to identify when an object is available for gc - you have either set it to null or you have "redirected" the variable that was originally referring to it, so that it now refers to a different object. if you have a reference to an object say, A and then you pass A as an argument to some constructor - new obj(A); - then even if you null your reference - A=null; - you can't say that A is available for gc.

eg, 1. obj = new Jo(); 2. obj.doSomething(); 3. obj = new Jo(); //Same as obj=null; 4. obj.doSomething(); The object referred by obj in line 1 becomes eligible for gc at line 4 ( anytime after line 3 has been executed).

References are not garbage collected and objects are. So if you have 2 references to the same object. 1. Object a = new Object(); 2. Object b=a; you now have 2 references to one object. The object has no name (it has a unique internal identity). Only the references have a name. So it is not correct to use the phrase. "a is available for gc". you should only say "the object referred to by "a" is available for gc" And this can happen when there are no references to an object. If the only reference to an object are instance (member) variables then when these variables are set to null, or set to other objects, then the objects those variables originally referenced will be available for gc.