9.1 Class (static) Variables and Methods

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Chapter 11 Classes Continued
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
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 10 Classes Continued
Static members Based on Java tutorial by Oracle: svars.html
Style It's still hard to give rules, but we can give some examples. Remember: this is to some extent a matter of taste.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
CSCI-383 Object-Oriented Programming & Design Lecture 14.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
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.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
More Object Concepts— Farrell, Chapter 4 Dr. Burns.
SEEM Java – Basic Introduction, Classes and Objects.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Class Fundamentals BCIS 3680 Enterprise Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Introduction to Object-oriented Programming
Classes and Objects Introduced
Lesson 10: Classes Continued
Sections Inheritance and Abstract Classes
Classes and Objects.
Examples of Classes & Objects
Inheritance and Polymorphism
Intro To Classes Review
Classes.
Section 11.1 Class Variables and Methods
More Object Oriented Programming
Can perform actions and provide communication
Can perform actions and provide communication
Week 6 Object-Oriented Programming (2): Polymorphism
Classes & Objects: Examples
CHAPTER 6 GENERAL-PURPOSE METHODS
Object Oriented Programming
Object Oriented Programming in java
Can perform actions and provide communication
Classes and Objects Part 2 Static Class Members and Arrays of Objects
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
Presentation transcript:

9.1 Class (static) Variables and Methods Use when information needs to be shared among all instances of a class The word “static” must be used during method and variable declaration

9.1 Class (static) Variables and Methods A class variable belongs to a class. Storage is allocated at program startup and is independent of the number of instances created. A class method is activated when a message is sent to the class rather than to an object.

9.1 Class (static) Variables and Methods Counting the Number of Students Instantiated Suppose we want to count all the student objects instantiated during the execution of an application. We introduce a variable, which we call studentCount. This variable is incremented in the constructor every time a student object is instantiated. Because the variable is independent of any particular student object, it must be a class variable.

9.1 Class (static) Variables and Methods Counting the Number of Students Instantiated In addition, we need two methods to manipulate the studentCount variable: one to initialize the variable to 0 at the beginning of the application and the other to return the variable's value on demand. These methods are called setStudentCount and getStudentCount, respectively. Because these methods do not manipulate any particular student object, they must be class methods.

9.1 Class (static) Variables and Methods Modifying the Student Class Following are the modifications needed to add the class variable and the two class methods in the Student class. public class Student {   private String name; ... rest of the instance variables go here ... public Student(){ studentCount++; // Increment the count when a student is // instantiated name = ""; test1 = 0; test2 = 0; test3 = 0; }

9.1 Class (static) Variables and Methods public void setName (String nm){ name = nm; } ... rest of the methods without change go here ...   //--------------- class variables and methods ---------------- static private int studentCount = 0; static public void setStudentCount(int count){ studentCount = count; static public int getStudentCount(){ return studentCount;

9.1 Class (static) Variables and Methods Here is some code that illustrates the new capabilities of the Student class: ... Student.setStudentCount (0); // Initialize count to 0 s1 = new Student(); // Instantiate a student s2 = new Student(); // Instantiate a student s3 = new Student(); // Instantiate a student System.out.print(Student.getStudentCount()); // Displays 3

9.1 Class (static) Variables and Methods Notice that class messages are sent to a class and not to an object. Also, notice that we do not attempt to manipulate the studentCount variable directly because, in accordance with the good programming practice of information hiding, we declared the variable to be private. In general, we use a static variable in any situation where all instances share a common data value. We then use static methods to provide public access to this data.

9.1 Class (static) Variables and Methods Rules for Using static Variables Class methods can reference only the static variables, and never the instance variables. Instance methods can reference static and instance variables.

9.1 Class (static) Variables and Methods Class Constants By using the modifier final in conjunction with static, we create a class constant. The value of a class constant is assigned when the variable is declared, and it cannot be changed later. To illustrate the use of class constants, we modify the Student class again by adding two class constants: MIN_SCORE and MAX_SCORE.

9.1 Class (static) Variables and Methods   //--------------- static variables and methods ---------------- static final public int MIN_SCORE = 0; static final public int MAX_SCORE = 100; }

9.1 Class (static) Variables and Methods Here is a segment of code that illustrates the Student class's new features: s = new Student(); s.setScore(1, -20); // Too small, will be set to MIN_SCORE s.setScore(2, 150); // Too large, will be set to MAX_SCORE s.setScore(3, 55); // Value is acceptable System.out.println (s);// Displays scores of 0, 100,and 55 System.out.println (Student.MIN_SCORE); // Displays 0 System.out.println (Student.MAX_SCORE); // Displays 100