Lecture 16 I can’t hear you through the static OO.

Slides:



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

COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
AAA. Today’s lecture Review of Chapter 6 Go over examples.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Multiple Choice Solutions True/False a c b e d   T F.
Applications in Java Towson University *Ref:
Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
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.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Libraries and APIs Don’t reinvent the wheel. What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
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.
Inheritance.
OOP: Encapsulation &Abstraction
Objects as a programming concept
I can’t hear you through the static
Scope and Code Generation
Classes and Inheritance
Java Course Review.
Use interfaces to define constants?
Agenda Warmup AP Exam Review: Litvin A2
More About Objects and Methods
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Object Oriented Programming
Dynamically Allocated Memory
Chapter 4 Procedural Methods.
Static and non-Static Chapter 5.
Java LESSON 7 Objects, Part 1
CSC 253 Lecture 8.
CSC 143 Inheritance.
Inheritance 2nd Lecture
CSC 253 Lecture 8.
CSC 113 Tutorial QUIZ I.
Lecture 10 List Richard Gesick.
Classes & Objects: Examples
Inheritance 2nd Lecture
More Object-Oriented Programming
Singleton Pattern Pattern Name: Singleton Pattern Context
Chapter 7 Procedural Methods.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Chapter 14 Abstract Classes and Interfaces
Object Oriented Programming
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Final and Abstract Classes
CMSC 202 Inheritance II.
Presentation transcript:

Lecture 16 I can’t hear you through the static OO

Coming up Static Types of methods –I–Instance –S–Static Example Calling Static methods Why Static Final

Static Have you heard the Java keyword “static” before? When? Where?

Coming up Static Types of methods –I–Instance –S–Static Example Calling Static methods Why Static Final

Instance or Static There are two types of methods: Instance – Instance methods are in an object and use the instance variables of that object. OO

Static – Static methods don’t use instance variables of the class they are defined in. – They tend to take all their data through arguments and return values based on that data OO

Coming up Static Types of methods –I–Instance –S–Static Example Calling Static methods Why Static Final

For Example The Math class contains a method for rounding numbers It does the same thing for any class that calls it Math.round(35.6) will return 36 The returned answer does not depend on the internal state of the Math object So why waste heap space making a Math object?

Static means no objects It is not possible to make a Math object – (The constructor is marked private, you can’t call it with the “new” keyword) Math is full of static methods Think of static methods living on the blueprint (the class) rather than in an object

ToolBox ToolBox is made up of Static methods This is ToolBox’s getRandomNumber() method public static int getRandomNumber(int max) { Random rand = new Random(); int number; number = rand.nextInt(max)+1; return number; }

Coming up Static Types of methods –I–Instance –S–Static Example Calling Static methods Why Static Final

To call To call a static method, you use the class name instead of the object name: int number = ToolBox.getRandomNumber(10);

Mixing A class can contain static methods and instance methods OO

Coming up Static Types of methods –I–Instance –S–Static Example Calling Static methods Why Static Final

Why Static? Static is useful when you want to make something like a utilities class: – Like Math, with rounding, flooring, trig functions etc – ToolBox, useful methods to support students – Arrays and Collections use static methods There is no need for an object OO

Static variables You can use static for variables too If you want all your objects to share a common number – Like the age of a litter of puppies Mark the age of the puppies as static and they will all refer to that same age. OO

Coming up Static Types of methods –I–Instance –S–Static Example Calling Static methods Why Static Final

Final variables Marking a variable final means you cannot change it when the program is running. Marking a variable as static final is as close as Java gets to global variables OO

Final isn’t just for variables Final method – final means it can’t be overridden. Final class – final means it can’t be extended OO

Summary Static Types of methods – Instance – Static Example Calling Static methods Why Static Final