CSC 212 – Data Structures Lecture 6: Static and non-static members.

Slides:



Advertisements
Similar presentations
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
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.
EC-241 Object-Oriented Programming
1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even.
Lecture 2: Object Oriented Programming I
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
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)
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.
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Applications in Java Towson University *Ref:
CSC 212 – Data Structures Lecture 12: Java Review.
Introduction to Object-Oriented Programming
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
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.
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.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Introduction to Programming Writing Java Beginning Java Programs.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program.
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Non-Static Classes What is the Object of this lecture?
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Question of the Day completes starts  What word completes the 1 st word & starts the 2 nd one: DON???CAR.
Topics Instance variables, set and get methods Encapsulation
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Question of the Day  Move one matchstick to produce a square.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Question of the Day  How do you add 5 matches to these 6 & make 9?
Lecture 3: Fields, Methods, & Constructors
Examples of Classes & Objects
Review Session.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Templates.
Pass by Reference, const, readonly, struct
More inheritance, Abstract Classes and Interfaces
Classes & Objects: Examples
class PrintOnetoTen { public static void main(String args[]) {
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CSG2H3 Object Oriented Programming
Presentation transcript:

CSC 212 – Data Structures Lecture 6: Static and non-static members

Using Methods Some methods need an object  What would Integer.intValue() return if you did not have an Integer object? But some methods do not make sense to use with an object  How to instantiate object to call main(String[])?  Should you need Integer to call Integer.parseInt()?

Static v. Non-static Methods Methods declared static do not need an instance on which to run  Behavior defined by object’s class, not any specific instance  Integer.parseInt() only depends on definition of “Integer” static methods use parameters, can return data or be void, and should have visibility modifier

Making a Method Static Method must include static in declaration public static void main(String args[]) public static int parseInt(String s) public static float max(float a, float b) Behaves (almost) like any other method  Parameters & locals work identically  Uses normal operators  Can call other methods

Static vs. Non-static Method Non-static methods have this parameter  Aliased to object on which method was called  Directly access fields & call other methods Static methods have no such parameter  Cannot directly access non-static fields or call non-static methods  Can directly use static fields and methods  Can access object’s fields and call its classes

BadDuck public class BadDuck { private int quackVolume; public static void printVolume(String[] args) { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ” + duck.getVolume()); } public int getVolume() { return quackVolume; } } Compiler error. Without an object, whose volume are we getting? This is fine. There is a duck for which we get the volume.

BadDuck public class BadDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak()); } } This compiles. Static methods do not include implicit “this” parameter, so we can call them like this. This is also fine. Since the method is static, however, we will not be able to use the instance.

Static Fields Some data do not belong to single objects, but class as a whole Float.MAX_VALUE Citizen.nationalPopulation Instructors.bestProfessor Static fields shared amongst all objects of type  All objects see the same value  Changing for one object changes for all objects

Static Field Example public class Player { private static int numPlayers; public static String topPlayer; private String name; public Player(String newName) { numPlayers += 1; name = newName; } public static int getNumPlayers() { return numPlayers; } public static String setTopPlayer(String name) { topPlayer = name; } } This is still bad. Fields should always * be made private.

Tracing With Statics public void startGame() { Player player1, player2, player3; player1 = new Player(“Homer Simpson”); player2 = new Player(“Joey JoJo Jr. Shabadoo”); player3 = player2; Player.topPlayer = “Homer Simpson”; player2.setTopPlayer(“Joey JoJo Jr. Shabadoo”); } Player numPlayers  0 topPlayer  “” statics name  “Homer Simpson” player1 numPlayers  1 statics name  “Joey JoJo Jr. Shabadoo” player2player3 numPlayers  2 topPlayer  “Homer Simpson” numPlayers  2 topPlayer  “Joey JoJo Jr. Shabadoo”

Your Turn Get back into groups and try daily activity

Before Next Lecture… Finish lab #1 Start week #3 assignment Get ready for programming assignment #1  It should be easy & you’ll have 2 weeks Wednesday’s lecture discusses I/O  Keep thinking & ask any questions you have  May want to review any old notes & handouts