Affordable Access

Lecture 16 - I can't hear you through the static

Authors
Publication Date

Abstract

Designing classes * * Lecture 16 I can’t hear you through the static OO * * Coming up Static Types of methods Instance 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 Instance 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 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 Instance 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 Instance 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 Instance Static Example Calling S

There are no comments yet on this publication. Be the first to share your thoughts.

Statistics

Seen <100 times
0 Comments

More articles like this

Can you hear me now?

on Journal of the Massachusetts D... 2013

Can you hear me now?

on Scientific American October 2010

"I can't hear you...".

on Nursing October 1977
More articles like this..