Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

This is Java Jeopardy Writing Methods…chapter 4…
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Lecture 2: Object Oriented Programming I
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
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 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Chapter 6: User-Defined Functions
Inheritance in the Java programming language J. W. Rider.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Exceptions in the Java programming language J. W. Rider.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Chapter 7 User-Defined Methods.
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
Presentation transcript:

Java Methods By J. W. Rider

Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy Method-like structures –Constructors, static initializers

Modularity in Java Packages are composed of one or more classes or interfaces. Classes and interfaces are composed of one more members ; either data members ( fields ) or function members ( methods ). Methods are composed of statements; either declarations ( local variables ) or executable statements.

Object-oriented methods Methods define the kinds of messages (and parameters) which objects of a particular class may receive. All object-oriented behavior is implemented through methods.

General method declarations Header qualifiers return-type method-name ( formal-parameters ) throws- clause Body { Statements; }

Application main() example public static void main(String[] args) { System.out.println(“Hello, world!”); }

Signature Method-name (formal-parameter-types) Does not include the return-type Within any class definition, the signature must be unique

Prototype Return-type method-name (formal-parameter- types) The prototype defines the kinds of messages to which the class or its instances respond.

Qualifiers Scope (external visibility) –public, protected, private static Miscellaneous –final, abstract, native

The static qualifier The static qualifier divides class members into two groups: –Those members which are shared by all instances of the class and exist whether or not any instances exist. –Those members which exist in individual objects and are independent from the same named members in other instances of the same class

Static constraints A static field defines a single variable shared by all instances of the class. A non-static field defines a variable that is replicated whenever a new instances of the class is constructed. A static method may not make an unqualified reference to any static member (field or method). A non-static method may make unqualified references to both static and non-static members. –May also use the reserved this variable.

The this variable Within each non-static method, Java automatically creates a reference variable named this that references the “current” instance of the class for which the method has been invoked. Whenever a non-static member is referenced within a non-static method, this is understood to be the target of the message. Every reference in Java must be handled through either a class (for static references) or through an object.

Return types The return type of a method may be any type used for a variable declaration. –These methods may be invoked wherever an expression is permitted in an executable statement or declaration. The return type of a method may also be void. –These methods may be invoked only by an executable statement.

The void type May only be used as a return-type for a method. –Do not try to use void as a variable type. Void indicates that the method does not return a value. –Such methods are used for their “side-effects,” such as output and state variable changes.

The return statement For void methods: –The return; statement is optional For other methods: –The method must terminate by a statement of the form “ return expression ; ” –The expression must evaluate to the same type as the return-type of the method

Formal parameters Variable declarations within the header of a method: –Enclosed by parentheses –Separated by commas –May not be initialized (no “default” parameter values in Java) –The types of the variables are used to identify the method –The names of the variables may only be used within the method body Even if no arguments are to be passed to a method, empty parentheses are used to distinguish between a field and method.

Method references Method declarations represent potential behavior. This behavior does not occur until the method is referenced elsewhere. methodname( actual-arguments )

Actual arguments Expressions, enclosed within parentheses and separated by commas. Java uses the expression types to determine which one of several overloaded methods of the same name should be invoked. Java uses the expression values as initializations for the formal parameters.

Invocation examples Void methods –doHello(); –object1.setInit(5); Return-value methods –x=object4.getInit(); –setInit(object2.getStatus()); // nesting –object3.getBase().doHello(); // chaining Static methods –obj5.method(); // implicit –MyClass.method(); // explicit

Multiple invocations At any given point in time, a single method may have been invoked through multiple locations in a program. Every invocation gets its own copy of the methods local variables (including the formal parameters) A method may even call invoke itself. This is called recursion.

Related structures Two other places where executable statements may be located: –Constructors –Static initializers

Constructors A constructor is invoked every time an instance of an object is created (e.g., with the new operator.) Constructor declarations are like ordinary method declarations with two important exceptions: –There is no return type; not even void –The name of the constructor is always the name of the class

Static initializers (Advanced!) Static initializers are invoked when a class is loaded into memory (before creating a single instance of the class) The header of a static initializer consists of the reserved word static Multiple static initializers may be declared within a class