{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 09 - CS133 Developing Programming Principles Lecture 9 All...

This preview shows pages 1–4. Sign up to view the full content.

1 CS133: Developing Programming Principles Lecture 9 All about static : static methods, static variables, main methods, constants, wrapper classes 2 CS133 Course Notes Lecture 9, Slide 2 Warning! Warning! Danger! You will use static methods sometimes: Math class main methods – Wrapper classes You will almost never write static methods You will almost never use or create static variables (except for constants) Don't overuse static!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3 CS133 Course Notes Lecture 9, Slide 3 static methods Belong to the class as a whole, not specific instances (objects) of it. Can be accessed without instantiating an object of the class type. 4 CS133 Course Notes Lecture 9, Slide 4 Temperature conversions // C == Celsius, F == Fahrenheit, K == Kelvin public class Temperature { public static double fToC(double fahrenheitTemp) { return (fahrenheitTemp – 32.0)*5.0/9.0; } public static double cToK(double celsiusTemp) { return celsiusTemp + 273.15; } public static double fToK(double fahrenheitTemp) { double degCelsius = fToC(fahrenheitTemp); return cToK(degCelsius); } // Similarly, write cToF(), kToC(),and kToF() }
5 CS133 Course Notes Lecture 9, Slide 5 Calling a static method • Syntax: ClassName . Method ( Arguments ); • Example: System.out.println(Temperature.fToC(-40.0)); Result: _________ System.out.println(Temperature.cToK(21.0)); Result: _________ System.out.println(Temperature.fToK(85.0)); Result: _________ 6 CS133 Course Notes Lecture 9, Slide 6 Invoking a non- static method in a static one static

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern