Objectives. Introduce static keyword examine syntax describe common uses

Similar documents
Introduction to Programming Using Java (98-388)

Assertions, pre/postconditions

C++ Important Questions with Answers

Common Misunderstandings from Exam 1 Material

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Lecture 10 Declarations and Scope

Object Oriented Programming in C#

Chapter 12: How to Create and Use Classes

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Variables. Data Types.

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Motivation was to facilitate development of systems software, especially OS development.

Selected Questions from by Nageshwara Rao

CS2141 Software Development using C/C++ C++ Basics

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Use the scantron sheet to enter the answer to questions (pages 1-6)

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Short Notes of CS201

CHAPTER 7 OBJECTS AND CLASSES

(CONDITIONALS_BOUNDARY)

Fundamental Concepts and Definitions

CS201 - Introduction to Programming Glossary By

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Introduction to C# Applications

PROGRAMMING FUNDAMENTALS

Motivation was to facilitate development of systems software, especially OS development.

Constructor in Java. What is a Constructor? Rules for create a Java Constructor

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Constants, References

Computer Programming, I. Laboratory Manual. Final Exam Solution

The Decaf Language. 1 Lexical considerations

Lab5. Wooseok Kim

Introduction to Java

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Operational Semantics of Cool

Pointers, Dynamic Data, and Reference Types

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

CS180 Recitation. More about Objects and Methods

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Points To Remember for SCJP

Domains: Move, Copy & Co

2 2

Objectives. Describe ways to create constants const readonly enum

C++ 8. Constructors and Destructors

Introduction to C++ Systems Programming

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Do not start the test until instructed to do so!

Classes Basic Overview

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Programming in C and C++

Java+- Language Reference Manual

Chapter 4 Defining Classes I

Function Overloading

Exceptions, Case Study-Exception handling in C++.

Language Reference Manual

The Decaf language 1

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

1.00 Lecture 8. Using An Existing Class, cont.

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Practice Questions for Chapter 9

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java Classes and Objects

Class, Variable, Constructor, Object, Method Questions

More About Objects and Methods

Object Oriented Modeling

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

An Introduction to C++

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Binghamton University. CS-140 Fall Data Types in Java

Interview Questions of C++

Type Conversion. and. Statements

CHAPTER 7 OBJECTS AND CLASSES

BASIC ELEMENTS OF A COMPUTER PROGRAM

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

THE BIG FOUR FRIEND FUNCTIONS

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

double128 Math Library (SDK) Quick Guide

! Two questions when we design an OO system : ! Our Initial goal: learn to design and implement simple objects.

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Transcription:

Static

Objectives Introduce static keyword examine syntax describe common uses 2

Static Static represents something which is part of a type rather than part of an object Two uses of static field method 3

Instance field Each object gets own copy of instance fields instance fields class Item public int ; public int ; public int Revenue; a Revenue instances Item a = new Item(); Item b = new Item(); Item c = new Item(); b Revenue c Revenue 4

Static field Only on copy of static field created using keyword static available even if no instances exist also called static variable instance fields class Item public int ; public int ; a static field public static int Revenue; b instances Item a = new Item(); Item b = new Item(); Item c = new Item(); c Revenue 5

Static field access Static field must be accessed through type name compiler error to attempt access through instance access using class name Item.Revenue = 5; Revenue 5 6

Meaning of static field Static field used to model shared resource roughly analogous to global variable in other languages a b Revenue c shared by all items 7

Static field initialization Three options for initialization of static fields default value static variable initializer static constructor 8

Static field default values Static fields set to default value 0 for numeric types false for bool '\x0000' for char null for references defaults to 0 class Item public static int Revenue; Revenue 0 9

Static variable initializer Can initialize static field in definition called static variable initializer initializer executed once executed in textual order Assigned value is limited can be literal, new statement, or return of static method call cannot call regular method initialize class Item public static int Revenue = -1; Revenue -1 10

Static constructor May supply static constructor to do initialization syntax is same name as type with keyword static only one allowed no parameters no access modifier can only access other static members class Item public static int Revenue; static constructor static Item() Revenue = -1; 11

Invocation of static constructor Static constructor invoked automatically not called directly Timing of execution unspecified but some guarantees given after static variable initializers before any instances are created before any static variables of that class are used at most once 12

Initialization application Instance and static initialization often used in same class pool shared by all Items instance gets a connection class Item private static ConnectionPool pool; private Connection connection; create pool get connection from pool return connection to pool static Item() pool = new ConnectionPool(5); public Item() connection = pool.acquire(); public void Dispose() pool.release(connection); 13

Static method Method can be static can only access static members cannot directly access non-static members no this object associated with call class Item private static int revenue; static method public static void ResetRevenue() revenue = 0; 14

Static method invocation Static method must be called through type name compile time error to attempt call through instance call static method Item.ResetRevenue(); 15

Static method application Static methods often useful for utility methods because global methods are not allowed for methods for which an instance is not needed public class Math public static double Sqrt(double d) public static double Sin (double a) public static double Log (double d) public class Console public static void WriteLine(string value) public static string ReadLine () 16

Summary Static represents things associated with a type rather than a particular instance of the type must be accessed through type name Static uses variables used to model shared resource methods useful when instance not needed in call Several options for initialization default values inline static constructor 17