AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.

Slides:



Advertisements
Similar presentations
Road Map Introduction to object oriented programming. Classes
Advertisements

Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Static members Based on Java tutorial by Oracle: svars.html
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Classes and Objects Including inheritance. OOP — Object-Oriented Programming In OOP system, the class is a fundamental unit. An OOP program models a.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
SEEM Java – Basic Introduction, Classes and Objects.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
CS1054: Lecture 11 More Sophisticated Behavior (contd..)
Your brain and the machine ACO101: Introduction to Computer Science.
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.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Non-Static Classes What is the Object of this lecture?
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
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.
9.1 Class (static) Variables and Methods
Methods Chapter 6.
Chapter 3: Using Methods, Classes, and Objects
Section 11.1 Class Variables and Methods
Using local variable without initialization is an error.
Unit-1 Introduction to Java
Simple Classes in Java CSCI 392 Classes – Part 1.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming in java
Presentation transcript:

AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods

AP Computer Science A – Healdsburg High School 2 Static Fields A static field (a.k.a. class field or class variable) is shared by all objects of the class. A non-static field (a.k.a. instance field or instance variable) belongs to an individual object.

AP Computer Science A – Healdsburg High School 3 Static Fields (cont’d) A static field can hold a constant shared by all objects of the class: A static field can be used to collect statistics or totals for all objects of the class (for example, total sales for all vending machines) public class RollingDie { private static final double slowDown = 0.97; private static final double speedFactor = 0.04;... Reserved words: static final

AP Computer Science A – Healdsburg High School 4 Static Fields (cont’d) Static fields are stored with the class code, separately from instance variables that describe an individual object. public static fields, usually global constants, are referred to in other classes using “dot notation”: ClassName.constName double area = Math.PI * r * r; setBackground(Color.BLUE); c.add(btn, BorderLayout.NORTH); System.out.println(area);

AP Computer Science A – Healdsburg High School 5 Static Fields (cont’d) Usually static fields are NOT initialized in constructors (they are initialized either in declarations or in public static methods). If a class has only static fields, there is no point in creating objects of that class (all of them would be identical). Math and System are examples of the above. They have no public constructors and cannot be instantiated.

AP Computer Science A – Healdsburg High School 6 Static Methods Static methods can access and manipulate a class’s static fields. Static methods cannot access non-static fields or call non-static methods of the class. Static methods are called using “dot notation”: ClassName.statMethod(...) double x = Math. random(); double y = Math. sqrt (x); double avgGPA = Student.computeSchoolGPA (); Vic.stackHasCD();

AP Computer Science A – Healdsburg High School 7 Static Methods (cont’d) public class MyClass { public static final int statConst; private static int statVar; private int instVar;... public static int statMethod(...) { statVar = statConst; statMethod2(...); instVar =...; instMethod(...); } Errors! OK Static method

AP Computer Science A – Healdsburg High School 8 Non-Static Methods A non-static method is called for a particular object using “dot notation”: objName.instMethod(...); Non-static methods can access all fields and call all methods of their class — both static and non-static. vendor.addMoney(25); die1.roll();