CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 1 CS100A, Lecture 7, 22 Sept. 1998 Static fields and methods. Suppose that we would like to main-

Slides:



Advertisements
Similar presentations
Introduction to Programming with Java, for Beginners dynamic vs. static static variables and methods.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Access to Names Namespaces, Scopes, Access privileges.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
UML Basics & Access Modifier
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CS100A, Fall Lecture 4 1 CS100A, Fall 1997 Lecture, Thursday, 12 September. This lecture continues the discussion of classes. In addition, the following.
Programming in Java CSCI-2220 Object Oriented Programming.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Objects and Classes Mostafa Abdallah
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Lecture 10:Static & Final Kenya © 2005 MIT-Africa Internet Technology Initiative MyMath Example public class MyMath { public double PI = ;
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Programming Languages and Paradigms Activation Records in Java.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
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.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
CompSci 230 S Programming Techniques
Topic: Classes and Objects
Classes and Objects Introduced
Java Memory Management
OOP: Encapsulation &Abstraction
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
Java Memory Management
Examples of Classes & Objects
CS100A, Lecture 7, 22 Sept Static fields and methods
Lecture 2 Objects and Classes
CS 302 Week 10 Jim Williams.
Chapter 9 Inheritance and Polymorphism
Interface.
Java Programming Language
Classes & Objects: Examples
Interfaces.
Sit next to someone. Today, we do some work in pairs.
CS2011 Introduction to Programming I Methods (II)
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Simple Classes in Java CSCI 392 Classes – Part 1.
Concepts for this lecture: Class Instance of a class, (an object)
Chapter 5 Classes.
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 1 CS100A, Lecture 7, 22 Sept Static fields and methods. Suppose that we would like to main- tain, somehow, the number of instances of Employee that were ever created. The following does not work! public class Employee { public int noEmps= 0; //No. of Emps //ever created public String name; // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; }

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 2 Using the prefix qualifier static means that there is only ONE field noEmps for the whole class, not one for each instance of the class: public class Employee { static public int noEmps= 0; //No. Empls //ever created public String name; //Employee’s name // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; } // Return no. of Employees ever created public int getNoEmps() {return noEmps;} }

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 3 static public void main (String args[ ]) { Employee v1= new Employee(); System.out.println("No. of emps" + v1.noEmps); Employee v2= new Employee(); System.out.println("No. of emps" + v2.noEmps); } Class Employee noEmps _______ Name GriesName Cardie Frame for main v1 ____ v2 ____

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 4 Model of Execution A static field (or method) is called a class variable (or method). At runtime, a box for a class named C con- tains all the class variables and methods. When- ever an instance of class C is created, it is placed in this box. A parameter or local variable declared as C v; is placed in the frame for the method in which it appears. This model assures that the rule we gave for finding a variable, when it is referenced, works: look first in the frame for method being execut- ed, then in the surrounding box, etc.

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 5 You can (should) use the name of the class, instead of a class variable, to refer- ence a static field or method Employee v1= new Employee(“Gries”); Employe v2= new Employee(“Cardie”); Instead of writing System.out.println(v1.noEmps); write System.out.println (Employee.noEmps);

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 6 Make the static field private, so it can’t be changed from outside the class. Have a “get” method to read its value. public class Employee { static private int noEmps= 0; //No. Empls //ever created public String name; //Employee’s name // Constructor -- An Employee named n public Employee(String n) { noEmps= noEmps+1; name= n; } // Return no. of Employees ever created public int getNoEmps() {return noEmps;} }

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 7 Method Color contains static fields (and methods) public class Color implements java.io.Serializable { public final static Color red = new Color(255, 0, 0); public final static Color white = new Color(255, 255, 255); public final static Color yellow = new Color(255, 255, 0); public final static Color pink = new Color(255, 175, 175); (The three arguments are RGB (red-green-blue) values) public String toString() { return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]"; }

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 8 A variable (or field) declared with prefix qualifier final may be assigned only once. Do it in the declaration. OKAY: final int x; x= 5; BETTER: final int x= 5;

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 9 Methods, as well as fields, may be static A static method may refer only to parameters, local variables, and static fields. It may not refer to non- static fields of the class. In which the method appears. // Return the number of Employees ever created static public int getNoEmps() {return noEmps;} A static method may be called only using the class name. It may not be called using a variable name. Employee v1= new Employee(“Gries”); WRONG: System.out.println(v1.getNoEmps()); RIGHT: System.out.println(Employee.getNoEmps());

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 10 Class Math contains ONLY static fields and methods public final class Math { public static final double E = ; public static final double PI = ; public static native double sin(double a); public static native double cos(double a); public static int round(float a) { return (int)floor(a + 0.5f); } public static int max(int a, int b) { return (a >= b) ? a : b; }

CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 11 On the PC, class Color (in file Color.java), and lots of other methods of the “abstract window toolkit”, should be in directory Program Files / Metrowerks / CodeWarrior / Java Support / Java Source / java / awt Class Math is in file Math.java in directory Program Files / Metrowerks / CodeWarrior / Java Support / Java Source / java / lang On the Mac, it will be in a similar place. DON’T CHANGE THESE FILES. But look at them. In Visual J++, highlight a class name and push button F1 to obtain a specification of the class.