11StaticMethodsAndVariables

11StaticMethodsAndVariables - CSE 114 Computer Science I...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables
Background image of page 1

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

View Full DocumentRight Arrow Icon
Static methods Remember the main method header? public static void main(String[] args) What does static mean? associates a method with a particular class name any method can call a static method either: directly from within same class OR using class name from outside class
Background image of page 2
Calling static methods directly example public class StaticCallerWithin { public static void main(String[] args) { String song = getSongName(); System.out.println(song); } public static String getSongName() { return "Straight to Hell"; } } Output? Straight to Hell
Background image of page 3

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

View Full DocumentRight Arrow Icon
Calling external static methods example public class StaticCallerFromOutside { public static void main(String[] args) { System.out.print("Random Number from 1-100: "); double randomNum = Math.random(); System.out.print(randomNum*100 + 1); } } What’s the method header for Math.random ? public static double random()
Background image of page 4
Static Variables We can share variables among static methods global variables How? Declare a static variable outside of all methods
Background image of page 5

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

View Full DocumentRight Arrow Icon
Static Variable Example public class MyProgram { static String myGlobalSong = new String("Jimmy Jazz“); public static void main(String[] args)
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2010 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 15

11StaticMethodsAndVariables - CSE 114 Computer Science I...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online