Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.

Slides:



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

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Objects and Classes The objectives of this chapter are: To discuss important aspects of the software development process To define objects and classes.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Road Map Introduction to object oriented programming. Classes
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
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.
Enhancing classes Visibility modifiers and encapsulation revisited
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
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.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Object-Oriented Programming in C++
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Session 7 Methods Strings Constructors this Inheritance.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Classes - Intermediate
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Core Java Garbage Collection LEVEL – PRACTITIONER.
Intro To Classes Review
Constructors CMSC 202.
Object Based Programming
Object Oriented Programming in java
Object Oriented Programming Review
Final Jim Brucker.
Object Oriented Programming in java
Web Design & Development Lecture 4
OO Programming Concepts
References Revisted (Ch 5)
Classes and Objects Object Creation
Chapter 7 Objects and Classes
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class variables and class methods To explain object deletion and the garbage collector

Will the following code work? Passing Primitives as Parameters public void method1() { int x = 50; int y = 100; swap(x,y); System.out.println("x is:" + x + " y is:" + y); } public void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; }

Parameters are passed by value in Java The code does not behave as intended. Passing Parameters by Value public void method1() { int x = 50; int y = 100; swap(x,y); } public void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; } 50 x: 100 y: 50 var1: 100 var2: 50 temp:

Passing Objects as Parameters It is also possible to pass object references as parameters to methods. The reference itself is passed by value The reference can be used to manipulate the target object Because of this behaviour, there are many classes that are defined as immutable or have instance variables which are immutable. This allows the object's reference to be passed without violating encapsulation. String is an example of an immutable class. It does not have a set method for the String data. String data can only be set upon initialization Many business oriented classes have attributes which cannot be changed after initialization Transaction Account

Passing Objects as Parameters public void method1() { Account anAccount = new Account(12345); initializeAccount(anAccount) } public void initializeAccount(Account theAccount) { Date openingDate = new Date(); theAccount.setOpeningDate(openingDate); [... more initialization...] } anAccount: Account AccountNum: theAccount:

The this reference For every instance method, there is a "hidden" parameter called "this" this is a reference to the instance (ie. object) upon which the method is being invoked this can be used to access instance variables (although "this" is implied) this can be returned from a method. public class Employee { private String name; public void setName(String aName) { this.name = aName; name = aName;// Equivalent }

Class Variables Instance variables belong to an instance (object) The compiler resolves the variable using the "this" reference. Java also allows for the creation of Class variables. Class variables belong to a Class Each time an instance is created, instance variables are created as well. Class variables are NOT created each time an instance is created. Class variables can be seen as global variables available to all instances of the class. To access a class variable, one does not need a "this" reference.

Class Variables - Declaration Class variables are declared as "static" in the class definition public class Employee { private static int employeeCount = 0; private String name; public Employee() { employeeCount++; } Employee 9 employeeCount:

Class Methods Instance variables are accessed through Instance methods Class variables are accessed through Class methods Instance methods are invoked on instances (ie. Objects) Class methods are invoked on classes Class methods do not have a "this" reference Class methods can only access Class variables Instance methods can access both Class variables and Instance variables. See the Java API Documentation for the Math class for examples.

Class Methods - Declaration Class methods are declared as "static" in the class definition public class Employee { private static int employeeCount = 0; public Employee() { employeeCount++; } public static int getEmployeeCount() { return employeeCount; } Employee employeeCount: Employee.getEmployeeCount(); 9

The "main" Method You may recall that the "main" method is declared as static This means that it is a class method It is invoked on a class, not an instance When the JVM is started, there are no instances of any class. Therefore, "main" must be static main does not have access to any instance variables. Usually, the responsibility of the main method is to instantiate objects public class HelloWorld { private String message = "Hello World"; public static void main(String[] args) { System.out.println(message);// ERROR! }

Redundant Code. One of the design goals of O-O is to reduce redundant code Method overloading actually encourages redundant code Redundant constructors means that initialization code will appear in multiple locations public class Account { private String owner; private int accountNumber; public Account() { owner = "Unknown"; accountNumber = 0; } public Account(String ownersName) { owner = ownersName; accountNumber = 0; } public Account(String ownersName, int anAccountNumber) { owner = ownersName; accountNumber = anAccountNumber; }

Using "this" to reduce code redundancy Constructors can invoke other constructors by using "this" as a method invocation It must be the first line of code in the constructor public class Account { private String owner; private int accountNumber; public Account() { this("Unknown", 0); } public Account(String ownersName) { this(ownersName, 0); } public Account(String ownersName, int anAccountNumber) { owner = ownersName; accountNumber = anAccountNumber; }

Garbage Collection Each object maintains a reference count. In order to use an object, one must have a reference to that object. If an object has no references, it is no longer usable. The garbage collector will reclaim any memory resources being consumed by unreferenced objects. The user/programmer has no control over when the garbage collector runs. It normally runs when: No other threads are running in the VM The amount of free space available goes below a threshold value.

Are memory leaks possible in Java? Many people, erroneously, believe that memory leaks are not possible in Java. They are possible, they are just more unlikely A memory leak can occur when two or more objects refer to each other, but there are no "external" references to any of those objects. As a guideline, avoiding bidirectional associations will help to avoid these kinds of memory leaks Object

Garbage Collection - finalize() Since objects can hold scarce resources (ie. open file, connection to database, etc), it is important to release those resources when an object is destroyed. When the garbage collector is ready to return an object's memory to the system, it invokes the object's finalize() method. Place any cleanup code in that method Remember, the programmer has no control over the garbage collector. As a result, the programmer has no control over when this method will be invoked (if ever).

Review Parameters are passed by value or by reference? What happens when an object is passed as a parameter to a method? What happens when an object is returned from a method? What is "this"? What are class variables and how are they defined? What are class methods? How does one constructor invoke another? What is garbage collection? What is the purpose of the finalize() method?