Object Initialization and Clean up Mehdi Einali Advanced Programming in Java 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
DATA STRUCTURES Lecture: Initialization & Cleanup Slides adapted from Prof. Steven Roehrig.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
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.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Programming Languages and Paradigms Object-Oriented Programming.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming Progamz pls. Importance VERY IMPORTANT.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Session 7 Methods Strings Constructors this Inheritance.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Objects and Classes Mostafa Abdallah
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
GC211 Data structure Lecture 3 Sara Alhajjam.
Lecture 6 Object Oriented Programming Using Java
Static data members Constructors and Destructors
Examples of Classes & Objects
Object Initialization and Clean up
Intro To Classes Review
Advanced Programming in Java
Advanced Programming in Java
اشیاء در جاوا Java Objects
Classes & Objects: Examples
Destructors.
Advanced Programming Behnam Hatami Fall 2017.
Assignment 7 User Defined Classes Part 2
CLEANING UP UNUSED OBJECTS
S.VIGNESH Assistant Professor/CSE, SECE
Code Animation Examples
Classes Lecture 7 from Chapter /1/11.
JAVA Constructors.
Assessment – Java Basics: Part 1
class PrintOnetoTen { public static void main(String args[]) {
Basics of OOP A class is the blueprint of an object.
SPL – PS4 C++ Advanced OOP.
Presentation transcript:

Object Initialization and Clean up Mehdi Einali Advanced Programming in Java 1

2 Agenda Init methods Constructors No Destructor Initialization Cleanup

3 Initialization An instantiated object, is not a ready object It may be and invalid object Person p = new Person(); p is an object without name, id and, … p is an invalid object It should be initialized

4 public class Student { //Mandatory private String name; private long id; //Optional private String homepage;... }

5 public void setName(String s) { if (s != null && !"".equals(s.trim()) && s.matches("[a-zA-Z ]+")) name = s; } public void setId(long idValue) { if (id > && id < ) id = idValue; } public void setHomepage(String addr) { homepage = addr; }

6 Initialization Method public void init(String name, long id) { setName(name); setId(id); }

7 Using the Object public static void main(String[] args) { Student st = new Student(); // st is an invalid object now st.init("Hossein Alizadeh", ); // st is initialized now. ready to be used System.out.println(st.getName()); System.out.println(st.getId()); }

8 Other Examples Circle c = new Circle(); c.init(12); Book b1 = new Book(); b1.init(“ من او ”, “ رضا اميرخانی ”); Book b2 = new Book(); b2.init(“ شاهنامه ”, “ ابوالقاسم فردوسی ”);

9 init() Method What are the disadvantages of init() method? Init method is invoked manually There is no guarantee for init invocation Before calling init method, the object has an invalid state

10 Constructors Constructor is a special method With the same name as the class Without any return type A constructor is called when an object is instantiated No invalid object

11 Constructor example

12 Default Constructor Constructors may have parameters Default constructor : no parameter Is implicitly implemented You can write your own default-constructor If you write any constructor, default implicit constructor is vanished.

13 Default Constructor Constructor

14

15 Constructors usually instantiate their properties public class Car { private Engine engine; private Tyre[] tyres; public Car() { engine = new Engine(); tyres = new Tyre[4]; for (int i = 0; i < tyres.length; i++) { tyres[i] = new Tyre(); } Who does destruct what constructors has built?

16 Destructor Java needs no destructor Destructor method in C++ Java has a finalize() method You can implement it for your class

17 Finalize method Java has no delete Java has no destructor Java has a special method: finalize finilize() is called when the object is garbage-collected If garbage collector is not invoked finalize() method is not called Why we may need finalize? Garbage collection is only about memory

18