1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

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.
Road Map Introduction to object oriented programming. Classes
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data.
Using final We use the notion of constant data to represent data that cannot be changed. public class Test { static final int someInt = 10; static final.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Chapter 5 Introduction to Defining Classes
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Java Memory Management
Java Memory Management
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
CS 302 Week 11 Jim Williams, PhD.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Web Design & Development Lecture 4
OO Programming Concepts
Chapter 7 Objects and Classes
CMSC 202 Constructors Version 9/10.
Presentation transcript:

1

2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter Passing

3 Types of Variables  There are four types of variables in Java: variabledeclarationscopelife-timecomment instance variable Outside all methods, not proceeded by keyword static Throughout the class definition Throughout the object life Property of each object static or class variable Outside all methods, proceeded by keyword static. Example: public static int numberOfCircles; Throughout the class definition Throughout the program life Property of the class. All objects of this class share one copy of the variable. local variable Within a method bodyThroughout its method Whenever its method is invoked Property of each object formal parameter In a method or constructor header Throughout its constructor or method Whenever its method is invoked Property of each object Note: Unnecessary use of static variables should be avoided.

4 Type of Variables (cont’d) public class Circle{ private static int numberOfCircles; private double radius; public Circle(double theRadius){ radius = theRadius; numberOfCircles++; } public void setRadius(double newRadius){ radius = newRadius; } public double getArea(){ double area = Math.PI * radius * radius; return area; } static variable instance variable formal parameter local variable formal parameter

5 Static vs. Instance Methods static or class methodinstance method DeclarationProceeded by static keyword. Example: public static double sqrt(double x) Not proceed by static keyword. Example: public double getGPA() How to call?ClassName.methodName(parameters) or objectName.methodName(parameters) Example: Math.sqrt(x) objectName.methodName(parameters) Example: student1.getGPA() What can be accessed? Can access constants and static variables; but cannot access instance variables. Can access constants, static variables, and instance variables. What can be referenced? Cannot refer to the this reference.Can refer to the this reference When to use?Implement a task that is not specifically related to an object or that does not need an object. Implement a particular behavior of an object.

6 The finalize method  A class whose objects need to perform some task when they are no longer referred to and are about to be garbage collected should redefine the finalize method of the Object class: void finalize()

7 Example class Circle{ private static int numberOfCircles; private double radius; public Circle(double theRadius){ radius = theRadius; numberOfCircles++; } public void finalize(){ numberOfCircles--; } public static int getCount(){ return numberOfCircles; } public class StaticTest{ public static void main(String[] args){ System.out.println(Circle.getCount()); Circle circle1 = new Circle(4.0); Circle circle2 = new Circle(2.5); System.out.println(circle2.getCount()); circle1 = null; /* Request the garbage collector to perform a garbage-collection pass */ System.gc(); System.out.println(Circle.getCount()); } Output: 0 2 1

8 toString Method  Prints the content of an object.  It is recommended to add toString() method when designing a new class.  When toString() is provided it is automatically invoked when object name is used where string is expected (print, concatenation)  When toString() is NOT provided, toString() of standard Object class is used, with the following format:  Example: class Student{ private int id; private String name; private double gpa; //... public String toString(){ return "Name: " + name + ", ID: " + id + ", GPA: " + gpa; } Student student = new Student(123, “Ahmad”, 3.5); System.out.println(student); Output: Name: Ahmad, ID: 123, GPA: 3.5

9 equals Method  Compares between the contents of two objects.  Operator == compares only references of two objects.  It is important to provide equals method when designing a class.  When this method is not provided the equals method of standard Object class is used (compares only the references)  Example: class Student{ private int id; private String name; private double gpa; public Student(int id, String name, double gpa){ this.id = id; this.name = name; this.gpa = gpa; } public boolean equals(Student student){ return this.id == student.id ; } public class TestStudent{ public static void main(String[] args){ Student s = new Student(123,"Ahmed",3.2); Student x = new Student(123,"Ahmed",3.2); Student y = new Student(456, “Yusuf”, 2.0); System.out.println(s.equals(x)); System.out.println(s == x); System.out.println(x.equals(y)); } Output: true false

10 Memory Model of JVM  To execute a Java program:  first compile it into bytecode,  then invoke the JVM which loads and executes it.  JVM divides the memory into three main areas:  Method Area:  The bytecode for each method in the class  The class variables (static variables)  Class information - modifies, etc  Methods' information - modifies, return type, etc  Heap:  Memory allocation for object instance variables  Reference to Method Area containing class data  Stack:  Keeps track of the order from one method call to another method area stack heap

11 Stack class A{ void method1(){ method2(); } void method2(){ method3(); method4 (); } void method3(){ statements ; ) void method4() statements ; }  Activation Record:  local variables and parameters  reference to objects created by the method  return address and return value of the method

12 Parameter Passing  Actual parameters of a method call may contain:  Simple or primitive data types (int, double, etc.)  Object References.  In case of simple data type:  The value is copied in corresponding formal parameters.  Thus, changing the value in the method will not affect the original value.  In case of object reference:  Another reference is created which points to the same object.  This other reference may be used to change the values of the object.

13 Parameter Passing Example public class StudentTest{ static int i = 10; public static void main(String[] args) { String str = "First Message"; Student s1 =new Student(123, "Khalid",1.3); Student s2=new Student(456, "Amr", 3.1); System.out.println("i = " + i); System.out.println("str = " + str); System.out.println("Student1: " + s1); System.out.println("Student2: " + s2); mixUp(i, str, s1, s2); System.out.println("i = " + i); System.out.println("str = " + str); System.out.println("Student1: " + s1); System.out.println("Student2: " + s2); } static void mixUp(int i, String str, Student one, Student two){ i++; str = "Second Message"; one = two; one.setGPA(3.4); one. setName("Ali"); } Output: i = 10 str = First Message Student1: Name: Khalid, ID: 123, GPA: 1.3 Student2: Name: Amr, ID: 456, GPA: 3.1 i = 10 str = First Message Student1: Name: Khalid, ID: 123, GPA: 1.3 Student2: Name: Ali, ID: 456, GPA: 3.4