SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
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.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Road Map Introduction to object oriented programming. Classes
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
Run-Time Storage Organization
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Java Review CIS 304 Intermediate Java Programming for Business.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Chapter Four Defining Your Own Classes continued.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Learners Support Publications Classes and Objects.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Session 7 Methods Strings Constructors this Inheritance.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CIS162AD – C# Call-by-Reference Methods 06_methods_by_ref.ppt.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
Class and Objects UNIT II.
Examples of Classes & Objects
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
User-Defined Functions
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
More Object Oriented Programming
C Passing arrays to a Function
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Defining Your Own Classes
Classes & Objects: Examples
Group Status Project Status.
Defining Classes and Methods
Chapter 6 Methods.
Chapter 7: User-Defined Functions II
Submitted By : Veenu Saini Lecturer (IT)
Lecture 11 Parameters CSE /26/2018.
Corresponds with Chapter 5
Presentation transcript:

SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3

SE-1010 Dr. Mark L. Hornick 2 A local variable is a variable that is declared within a method declaration Local variables are accessible only from the method in which they are declared. In this example, output and message are local variables accessible only from within printSomeMessage public void printSomeMessage( String message ) { // textMessage is a local String variable String output = “The message is “ + message ; System.out.println(output ); }

SE-1010 Dr. Mark L. Hornick 3 In this example, output is a local variable public class Student { private String firstName; … public String toString(){ String output; output = “My name is ” + firstName; return output; } } Local variables are only visible to the method in which they are defined.

SE-1010 Dr. Mark L. Hornick 4 In this example, x, y, and z are all local variables public int add( int x, int y ){ int z = x + y; return z; } Local variables are only visible to the method in which they are defined.

SE-1010 Dr. Mark L. Hornick 5 Rules for Local Variables A local variable is a variable that is declared within a method declaration. The parameters of a method are also considered local variables to the method. Local variables are accessible only from the method in which they are declared. Memory space for local variables is allocated only during the execution of the method. When the method execution completes, memory space will be cleared.

SE-1010 Dr. Mark L. Hornick 6 A local variable with the same name as a class attribute will hide the attribute within the scope of the local variable public class Student { private String someString; // attribute … public void method1(){ String someString; // local var hides attribute someString = “hello”; // uses local variable } public void method2(){ someString = “se1010”; // uses attribute } Local variables are only visible to the method in which they are defined.

SE-1010 Dr. Mark L. Hornick 7 The this keyword is used to distinguish an object’s attribute value from a local variable in the case of ambiguity public class Student { private String someString; // attribute … public void method1(){ String someString; // local var hides attribute someString = “hello”; // uses local variable this.someString = “there” // uses attribute for “this” object } public void method2(){ someString = “se1010”; // uses attribute } The best approach, however, is to avoid local variables that have the same name as attributes.

SE-1010 Dr. Mark L. Hornick 8 A local variable is a variable that is declared within a method declaration Local variables are accessible only from the method in which they are declared. In this example, output and message are local variables accessible only from within printSomeMessage public void printSomeMessage( String message ) { // textMessage is a local String variable String output = “The message is “ + message ; System.out.println(output ); }

SE-1010 Dr. Mark L. Hornick 9 Passing primitive arguments to methods When a method containing primitive arguments is called, the value of the actual argument is passed to the matching formal argument, and separate memory space (the Stack) is allocated to store this value. This way of passing the value of arguments is called a pass-by-value, or call-by-value, scheme.

SE-1010 Dr. Mark L. Hornick 10 Example of pass-by-value/call-by-value argument passing from caller to called method // this method is part of the main class public static void main(String args[]) { Calculator calc1 = new Calculator(); int sum = calc1.add( 3, 4); }.... // This method is part of the Calculator class public int add( int x, int y) { return x+y; // return the sum } The values 3 and 4 are passed as the actual arguments The formal arguments x and y receive copies of the values provided by the caller The value resulting from the addition of x+y is passed back to the caller, where it is copied into the variable sum

SE-1010 Dr. Mark L. Hornick 11 The datatype of an argument must be assignment-compatible with the data type of the matching formal parameter When it can, the compiler will provide automatic upcasts of the argument to the datatype of the matching parameter For example, if you supply an int argument where a long parameter is expected, the compiler will upcast the int to a long But the compiler will complain if you try to pass a long argument into an int parameter  This is called a downcast, and will not be supplied by the compiler

SE-1010 Dr. Mark L. Hornick 12 Passing and Returning Objects Passing and returning objects follows the same process as passing and returning primitive data types. The only difference is that with objects, the value being passed is the reference (or address) to an object. When a variable is an object name, the reference of the variable is the address in memory where the object is stored.

SE-1010 Dr. Mark L. Hornick 13 Review: Primitive variables vs. Object variables The only difference between a variable for a primitive and a variable for objects is the contents in the memory locations. For primitives, a variable contains the numerical value itself. For objects, a variable contains an address where the object is stored. 10 name x String object

SE-1010 Dr. Mark L. Hornick 14 Example of pass-by-reference, or call- by-reference // this method is part of the main class public static void main(String args[]) { String first = “Joe”; student.setFirstName( first ); }... // This method is part of the Student class public void setFirstName( String name) { firstname = name; } The identifier first references a String object and is passed as an actual argument The formal argument name receives the reference to the String object

SE-1010 Dr. Mark L. Hornick 15 Another example of pass-by- reference, or call-by-reference // this method is part of the main class public static void main(String args[]) { String s = student.getFirstName( ); }... // This method is part of the Student class public String getFirstName( ) { return firstName; } The identifier s receives a reference to a String object The method returns a copy of the reference to (the address of) the String object containing the name

SE-1010 Dr. Mark L. Hornick 16 Dot notation When you call a public method of an object, you must use dot notation to specify both the method and the object you are calling it on Dot notation is optional when you call a method from another method if the two methods belong to the same object. If dot notation is used, use the reserved word this to refer to the same object.

SE-1010 Dr. Mark L. Hornick 17 Calling a method belonging to the same object vs. calling a method belonging to a different object.