Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.

Slides:



Advertisements
Similar presentations
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Advertisements

1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Return values.
Java Programming Abstract classes and Interfaces.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
More About Objects and Methods
Numerical Data Recitation – 01/30/2009
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Applications in Java Towson University *Ref:
CSE 114 – Computer Science I Strings, I/O, and Methods Bonneville Salt Flats, Utah.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Object Oriented Programming I
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
Lecture 10:Static & Final Kenya © 2005 MIT-Africa Internet Technology Initiative MyMath Example public class MyMath { public double PI = ;
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Creating and Using Class Methods. Definition Class Object.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Non-Static Classes What is the Object of this lecture?
ICS 3U Math Class. ActionScript 3 – Math Class Rounding ceil – closest integer >= number (rounds up) floor – closest integer
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Examples of Classes & Objects
Interface.
Chapter 3 Methods.
User-Defined Functions
Static and non-Static Chapter 5.
CMSC 202 Static Methods.
Classes & Objects: Examples
Chapter 5 Methods.
Variables, Types, Operations on Numbers
Variables, Types, Operations on Numbers
Building Java Programs
Today in COMP 110 Brief static review The Math Class Wrapper Classes
More About Objects and Methods Chapter 5 Programming with Methods
Using java libraries CGS3416 spring 2019.
Visibilities and Static-ness
Presentation transcript:

Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables

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

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

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* ); } What’s the method header for Math.random ? public static double random()

Static Variables We can share variables among static methods –global variables How? –Declare a static variable outside of all methods

Static Variable Example public class MyProgram { static String myGlobalSong = new String("Jimmy Jazz“); public static void main(String[] args) { System.out.println(myGlobalSong); changeSong("Spanish Bombs"); System.out.println(myGlobalSong); } public static void changeSong(String newSong) { myGlobalSong = newSong; } Output? Jimmy Jazz Spanish Bombs

Uses for Static Methods Static methods are commonly used to provide libraries of useful and related functions Examples: –the Math class has useful one and done methods functions include pow, sqrt, max, min, etc. Also used for defining applications

The Math Class Includes constants –Math.PI (approximately ) –Math.E (base of natural logarithms, approximately 2.72) Includes three similar static methods: –round, floor, and ceil Math.round returns the nearest whole number –Math.round(3.3) returns 3 –Math.round(3.7) returns 4 Math.floor returns the nearest whole number (as a double ) that is equal to or less than its argument –Math.floor(3.3) returns 3.0 –Math.floor(3.7) returns 3.0 Math.ceil (short for ceiling) returns the nearest whole number (as a double ) that is equal to or greater than its argument –Math.ceil(3.3) returns 4.0 –Math.ceil(3.7) returns 4.0

static methods Example public class Circle { public static final double PI = ; public static double area(double diameter) { return PI*diameter*diameter/4.0; } public static double circumference (double diameter) { return PI*diameter; }

public class CircleDemo { public static void main(String[] args) { PrintStream o = System.out; double diameter; o.print ("Input a diameter: "); diameter = keyboard.nextDouble(); o.println("The area is “ + Circle.area(diameter)); o.println("The circumference is “ + Circle.circumference(diameter)); }

Static Variables Not instance variables –For instance variables, each instantiated object has its own instance variables w/ values –For a static variable, an entire class shares a variable May be public or private but are usually private for the same reasons instance variables are. Only one copy of a static variable and it can be accessed by any object of the class. Why use static variables? –Can be used to let objects of the same class coordinate information, like counting the number of objects

public class NamedPerson { private String name; private static int numPeople = 0; public NamedPerson(String initName) { name = initName; numPeople++; } public String getName() { return name; } public static int getNumPeople() { return numPeople; } } Example Using static variables

public class StaticVariablesDriver { public static void main(String[] args) { System.out.println(peopleToString()); NamedPerson joe = new NamedPerson("Joe"); System.out.println(peopleToString()); NamedPerson bob = new NamedPerson("Bob"); System.out.println(peopleToString()); } public static String peopleToString() { return "So far, " + NamedPerson.getNumPeople() + " people have been constructed."; } OUTPUT:So far, 0 people have been constructed. So far, 1 people have been constructed. So far, 2 people have been constructed.

Static/Non-static Rules Static methods –cannot directly reference instance variables –can directly reference static variables –cannot directly call non-static methods –can directly call static methods Non-Static methods –can directly reference instance variables –can directly reference static variables –can directly call non-static methods –can directly call static methods

Which lines have errors? public class StaticTester {int N; static int X; public void printN() {System.out.print(N);} public static void printX(){System.out.print(X);} public void nonStaticMethod() {N = 0; X = 0; printN(); printX(); } public static void staticMethod() {N = 0; X = 0; printN(); printX(); }