I%20can - PowerPoint PPT Presentation

About This Presentation
Title:

I%20can

Description:

Static methods don't use instance variables of the class they are ... number = rand.nextInt(max) 1; return number; Coming up. Static. Types of methods. Instance ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 21
Provided by: ter80
Category:
Tags: 20can | max1

less

Transcript and Presenter's Notes

Title: I%20can


1
Lecture 16
OO
  • I cant hear you through the static

2
Coming up
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final

3
Static
  • Have you heard the Java keyword static before?
  • When?
  • Where?

Buzz Groups
4
Coming up
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final

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

6
Static
OO
  • Static
  • Static methods dont 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

7
Coming up
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final

8
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?

9
Static means no objects
  • It is not possible to make a Math object
  • (The constructor is marked private, you cant
    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

10
ToolBox
  • ToolBox is made up of Static methods
  • This is ToolBoxs getRandomNumber() method
  • public static int getRandomNumber(int max)
  • Random rand new Random()
  • int number
  • number rand.nextInt(max)1
  • return number

11
Coming up
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final

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

13
Mixing
OO
  • A class can contain static methods and instance
    methods

14
Coming up
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final

15
Why Static?
OO
  • 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

16
Static variables
OO
  • 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.

17
Coming up
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final

18
Final variables
OO
  • 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

19
Final isnt just for variables
OO
  • Final method
  • final means it cant be overridden.
  • Final class
  • final means it cant be extended

20
Summary
  • Static
  • Types of methods
  • Instance
  • Static
  • Example
  • Calling Static methods
  • Why Static
  • Final
Write a Comment
User Comments (0)
About PowerShow.com