Object Initialization and Clean up

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.
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
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.
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,
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.
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.
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.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Object Initialization and Clean up Mehdi Einali Advanced Programming in Java 1.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
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.
Constructors and Destructors
GC211 Data structure Lecture 3 Sara Alhajjam.
Advanced Programming in Java
Advanced Programming in Java
Static data members Constructors and Destructors
Examples of Classes & Objects
Interface.
Intro To Classes Review
Review Session.
ATS Application Programming: Java Programming
Advanced Programming in Java
Advanced Programming in Java
Java LESSON 7 Objects, Part 1
Class Inheritance (Cont.)
اشیاء در جاوا Java Objects
Enumerations & Annotations
Simple Classes in C# CSCI 293 September 12, 2005.
Classes & Objects: Examples
Advanced Programming Behnam Hatami Fall 2017.
Constructors and Destructors
CLEANING UP UNUSED OBJECTS
S.VIGNESH Assistant Professor/CSE, SECE
Advanced Programming in Java
Code Animation Examples
Classes Lecture 7 from Chapter /1/11.
JAVA Constructors.
Advanced Programming in Java
Enumerations & Annotations
class PrintOnetoTen { public static void main(String args[]) {
Basics of OOP A class is the blueprint of an object.
Chapter 9 Objects and Classes Part 01
Chapter 11 Inheritance and Polymorphism Part 1
OO Programming Concepts
Presentation transcript:

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

Agenda Init methods Constructors Cleanup

Init method

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

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

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 > 10000000 && id < 100000000) id = idValue; public void setHomepage(String addr) { homepage = addr;

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

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

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

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

constructors

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

Constructor example

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.

Default Constructor Constructor

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?

finilize

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

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

end