A Second Look at Classes and Objects - 1 Static Class Members

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

Lesson 14 Classes, Continued CS1 Lesson More Classes1.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 6: A First Look at Classes
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Starting Out with Java: From Control Structures through Objects.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 9: A Second Look at Classes and Objects
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.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
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:
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Some odds and ends about Classes and Objects. 6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CITA 342 Section 1 Object Oriented Programming (OOP)
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 9 A Second Look at Classes and Objects - 2.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Programming Logic and Design Seventh Edition
Chapter 3 Classes & Objects
Chapter 9 More on Objects and Classes
A first Look at Classes.
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Chapter 3: Using Methods, Classes, and Objects
Classes & Objects.
Introduction to Classes
A Second Look at Classes and Objects - 2
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming Using C++
Chapter 6: A First Look at Classes
Chapter 9: A Second Look at Classes and Objects
Introduction to Classes
Introduction to Classes
Chapter 6 Methods: A Deeper Look
Object Oriented Programming in java
Classes and Objects.
Today’s topics UML Diagramming review of terms
Assessment – Java Basics: Part 1
Lecture 5- Classes, Objects and Methods
Submitted By : Veenu Saini Lecturer (IT)
Lecture 8 Object Oriented Programming (OOP)
Chapter 6: A First Look at classes
Presentation transcript:

A Second Look at Classes and Objects - 1 Static Class Members Chapter 9 A Second Look at Classes and Objects - 1 Static Class Members

Contents A Quick Review of Instance Fields and Instance Methods Static Class Members Static Fields Static Methods

I. A Quick Review of Instance Fields and Instance Methods Each instance of a class has its own set of fields, which are known as instance fields. We can create several instances of a class and store different values in each instance's fields. Classes may have instance methods as well. When we call an instance method, it performs an operation on a specific instance of the class.

I. A Quick Review of Instance Fields and Instance Methods Class Rectangle Rectangle - length - width + setLength() + setWidth() + getLength() + getWidth() + getArea()

Rectangle box = new Rectangle(); box.setLength(10.0); box.setWidth(20.0); A Rectangle object The box variable holds the address of a Rectangle bject. length width 0.0 address 0.0 A Rectangle object The box variable holds the address of a Rectangle bject. length width 10.0 address 0.0 A Rectangle object The box variable holds the address of a Rectangle bject. length width 10.0 address 20.0

Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle(); The kitchen variable holds the address of a Rectangle bject. length width 0.0 A Rectangle object address 0.0 The bedroom variable holds the address of a Rectangle bject. length width 0.0 A Rectangle object address 0.0

kitchen. setLength(10. 0); kitchen. setWidth(14. 0); bedroom kitchen.setLength(10.0); kitchen.setWidth(14.0); bedroom.setLength(15.0); bedroom.setWidth(12.0); The kitchen variable hold the address of a Rectangle object. A Rectangle object A Rectangle object Length 1 width address address 10.0 14.0 The bedroom variable hold the address of a Rectangle object. A Rectangle object A Rectangle object Length width address address 15.0 12.0

II. Static Class Members It is possible to create a field or method that does not belong to any instance of a class. Such members are known as static fields and static methods. When a value is stored in a static field, it is not stored in an instance of the class. In fact, an instance of the class doesn't even have to exist in order for values to be stored in the class's static fields.

II. Static Class Members Static methods do not operate on the fields that belong to any instance of the class. Instead, they can operate only on static fields. We can think of static fields and static methods as belonging to the class instead of an instance of the class.

II.1. Static Fields A static field is declared with the keyword static after the access specifier. There is only one copy of the field in memory, regardless of the number of instances of the class that might exist. A simple copy of a class's static field is shared by all instances of the class.

II.1. Static Fields Problem: Count the number of instances of a class that are created. Use a static field to keep count of the number of instances. Each time an instance of the class is created, the constructor will be called and that static field will be incremented.

instanceCount field (static) II.1. Static Fields All instances of the class Countable share the static field instanceCount. instanceCount field (static) 3 object1 object2 object3

II.1. Static Fields Java automatically stores 0 in all uninitialized static member variables. The instanceCount field in this Countable class is explicitly initialized so it is clear to anyone reading the code that the field starts with the value 0.

II.2. Static Methods A static method is declared with the keyword static after the access specifier in the method header. When a class contains a static method, it is not necessary for an instance of the class to be created in order to execute the method. Static methods belong to the class and may be called without any instances of the class. In order to call a static method, we simply write the name of the class before the dot operator in the method call.

II.2. Static Methods

II.2. Static Methods

II.2. Static Methods Static methods are convenient for many tasks because they can be called directly from the class, as needed. They are most used to create utility classes that perform operations on data, but have no need to collect and store data. Static methods can not refer to non-static members of the class. Any method called from a static method must also static. If the static method uses any of the class's fields, they must be static as well.

II.2. Static Methods

II.2. Static Methods

Checkpoint 9.1 What is the difference between an instance field and a static field? 9.2 What action is possible with a static method that is not possible with an instance method? 9.3 Describe the limitation of static methods.