Static members Based on Java tutorial by Oracle: svars.html

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Inheritance, part 2: Subclassing COMP 401, Fall 2014 Lecture 8 9/11/2014.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
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.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
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)
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
Chapter 4 Objects and Classes.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
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:
Java Programming static keyword.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
Arithmetic, Class Variables and Class Methods Week 11
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Introduction to Object-Oriented Programming Lesson 2.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Class Fundamentals BCIS 3680 Enterprise Programming.
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.
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Introduction to Object-oriented Programming
Lecture 3: Introduction to Object and Classes
Static data members Constructors and Destructors
9.1 Class (static) Variables and Methods
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Java Primer 1: Types, Classes and Operators
Road Map Introduction to object oriented programming. Classes
Section 11.1 Class Variables and Methods
METHODS AND BEHAVIORS AKEEL AHMED.
Chapter 6 Methods: A Deeper Look
Singleton Pattern Pattern Name: Singleton Pattern Context
Static in Classes CSCE 121 J. Michael Moore.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Previous Lecture: Today’s Lecture: Reading (JV):
Threads and concurrency / Safety
Presentation transcript:

Static members Based on Java tutorial by Oracle: svars.html svars.html And on: methods.html

Motivation The fields we’ve encountered so far are unique for every object instantiated. data behaviorall instances Sometimes there’s a need for a data or a behavior which is shared by all instances of a class. For example: counting the number of instances of a certain class in the program. Introduction to Computer Science, Ben- Gurion University of the Negev 2

Static fields static fieldsclass variables. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. Introduction to Computer Science, Ben- Gurion University of the Negev 3

Example For example, suppose you want to create a number of Bicycle objects and assign each a serial number. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. Introduction to Computer Science, Ben- Gurion University of the Negev 4

Example cont’d Introduction to Computer Science, Ben- Gurion University of the Negev 5 public class Bicycle{ private int cadence; private int gear; private int speed; // An instance variable for the object ID private int id; // A class variable for the number of Bicycle objects instantiated private static int numberOfBicycles = 0;... }

Example cont’d Introduction to Computer Science, Ben- Gurion University of the Negev 6 public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; // increment number of Bicycles and assign ID number id = ++numberOfBicycles; } public int getID() { return id; }

Example cont’d Class variables are referenced by the class name itself, as in Bicycle.numberOfBicycles This makes it clear that they are class variables. Reference through an object is possible, but is discouraged. Introduction to Computer Science, Ben- Gurion University of the Negev 7

Static methods The Java programming language supports static methods as well as static variables. main main method is static, since it must be accessible for an application to run, before any instantiation takes place. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in ClassName.methodName(args) Introduction to Computer Science, Ben- Gurion University of the Negev 8

Static methods A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the static field numberOfBicycles : Introduction to Computer Science, Ben- Gurion University of the Negev 9 public static int getNumberOfBicycles() { return numberOfBicycles; }

Static methods Not all combinations of instance and class variables and methods are allowed: Instance methods can access instance variables and instance methods directly. Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. this super Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this or super keywords, as there is no instance to refer to. Introduction to Computer Science, Ben- Gurion University of the Negev 10

Constants Constant fields are often declared as static. For example NUM_OF_WHEELS which is a characteristic of any Bicycle, and not of a certain instance. If there’s a sense to expose a constant field to the outside world, it is common to declare the field as public, rather than through a getter. Introduction to Computer Science, Ben- Gurion University of the Negev 11

Singleton pattern Restricting the instantiation of a certain class to exactly one object. This is useful when exactly one object is needed to coordinate actions across the system. A singleton object is accessible globally using a static method. Introduction to Computer Science, Ben- Gurion University of the Negev 12

Singleton Introduction to Computer Science, Ben- Gurion University of the Negev 13 public class Controller { private static Controller instance = null; private Controller () {... } public static Controller getInstance() { if (instance == null) { instance = new Controller (); } return instance; }... } * Not thread-safe Notice the private constructor

A static block The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM. A static block helps to initialize the static data members, just like constructors help to initialize instance members. Introduction to Computer Science, Ben- Gurion University of the Negev 14

A static block Introduction to Computer Science, Ben- Gurion University of the Negev 15 public class Controller { private static Controller instance; static { instance = new Controller (); } private Controller () { } public static Controller getInstance() { return instance; }... }

Summary static variable belongs to theclassnot object It is a variable which belongs to the class and not to object (instance). initialized only once Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. singlecopy A single copy to be shared by all instances of the class. accesseddirectlyclass name A static variable can be accessed directly by the class name and doesn’t need any object Syntax :. Introduction to Computer Science, Ben- Gurion University of the Negev 16

Summary static method belongs to the classnot object It is a method which belongs to the class and not to the object (instance). can access only static data A static method can access only static data. It can not access non-static data (instance variables). can call onlystatic methods A static method can call only other static methods and can not call a non-static method from it. thissuper A static method cannot refer to "this" or "super" keywords in anyway. accessed directlyclass name A static method can be accessed directly by the class name and doesn’t need any object Syntax :. Introduction to Computer Science, Ben- Gurion University of the Negev 17