Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
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.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Overloaded Constructors constructors can be overloaded, like other methods – i.e., a class can define several constructors all constructors must have the.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
* Calls to methods may have implicit parameters. * Explicit parameters may not be needed because the method has the implicit parameter to work with. *
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
4.1 Instance Variables, Constructors, and Methods.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CIT 590 Intro to Programming Lecture 13. Agenda A few topics that you have seen but might not have fully grasped Static Public, private, protected etc.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
The Math Class Methods Utilizing the Important Math Operations of Java!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Interfaces and Inner Classes
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Topics Instance variables, set and get methods Encapsulation
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
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.
Static data members Constructors and Destructors
Some Eclipse shortcuts
CS 302 Week 11 Jim Williams, PhD.
CMSC 202 Static Methods.
Constructor Overloading
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Interface.
slides created by Ethan Apter
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
slides created by Ethan Apter
Simple Classes in Java CSCI 392 Classes – Part 1.
Namespaces, Scopes, Access privileges
Classes and Objects Static Methods
Java Programming Language
slides created by Ethan Apter and Marty Stepp
Visibilities and Static-ness
Chapter 7 Objects and Classes
Presentation transcript:

Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation provided an example where neither an implicit nor an explicit parameter were required. The methods in the Math class exemplify the more typical case, where some useful computation is done on explicit parameters and the result is returned.

Static Methods, cont. These useful methods belong to the class that contains them. They may be written to take simple types or object references of any class as explicit parameters. Calls to static methods take this general form: result = ClassName.method(parameter(s)); A specific example that we’ve seen is: double x, y; … y = Math.sqrt(x);

The main() method is a static method The key word “static” is used when declaring such methods. You have seen this used in writing programs: public class MyProg { public static void main(String[] args) { … The main() method in programs is a static method. The program class itself is simply a container for this useful method. The class does not have a constructor. The system calls the static method directly in order to run the program.

Creating static methods It is possible to write your own static methods that take explicit parameters and do some calculation or produce some other desired result. Here is a brief example: public class MyMethodContainer { public static double findPercent(double first, double second) { return ((first / second) * 100.0); }

Example of using static methods Assuming that the MyMethodContainer class is in the same package as the program, here is a small program that would use the static method: public class TestContainer { public static void main(String[] args) { double x = 15; double y = 20; double percentResult = MyMethodContainer.findPercent(x, y); System.out.println(x + “ is “ + percentresult + “ % of “ + y); } You would expect to get 75.0% as the result.

Static method with object parameters The class TestContainer might also contain a method like this, which illustrates taking an object reference as a parameter. public static double findPercent(Cup5 first, double second) { return ((first.getSeedCount() / second) * 100.0); }

Static variables In addition to static methods, Java supports static variables. These are variables which are declared in a class but which differ from instance variables in this way: There is only one copy of the variable, and it is maintained in the class; objects, namely instances of the class, do not receive separate copies of this variable.

Static variables, cont. The static variable may be declared private. If so, it is not directly accessible from outside the class. However, it is directly accessible to every object of the class. A static variable may also be declared public. You have already encountered such variables. The mathematical constants PI and E in the Math class are public static final doubles.

Example of Static Variables It would be possible to declare a minimum seedCount for objects of the cup class. The example below shows the declaration and initialization of this variable. It is declared final. That means that the value of the static variable can’t be changed.

Example of Static Variables, cont. Since there is only one copy of the minimumSeedCount variable for the whole class, this variable can be initialized when it is declared. It doesn’t make sense to assign it a value in a constructor that is called every time a new object of the class is created. The example also shows a constructor and the setSeedCount() method rewritten so that the value of seedCount couldn’t go below the value of the static variable.

Example of Static Variables, cont. public class Cup6 { private int seedCount; private static final int minimumSeedCount = 0; … public Cup6() { seedCount = minimumSeedCount; } … public boolean setSeedCount(int seedCountIn) { if(seedCountIn >= minimumSeedCount) { seedCount = seedCountIn; return true; } else return false; } … }

A static variable without final It would also be possible to declare and initialize the static variable so that it isn’t final: private static int minimumSeedCount = 0; Then a static method could be added to the class that would allow the minimum to be changed: public static void changeMinimum(int newMin) { minimumSeedCount = newMin; } Notice that this method does not change the instance variable for an object. It changes the minimumSeedCount that the class maintains for all objects of the class.

Be careful with certain design changes By adding this static instance, it is no longer suitable to construct instances with a seedCount of initially zero. So the default constructor (the constructor with no arguments) that does this would need to be removed The constructor would probably look something like this: public Cup6(int seedCountIn) { if(seedCountIn >= minimumSeedCount) seedCount = seedCountIn; else seedCount = minimumSeedCount; }