Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }

Slides:



Advertisements
Similar presentations
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
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.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
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.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture 2: Classes and Objects, using Scanner and String.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Session 7 Methods Strings Constructors this Inheritance.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Programming Languages and Paradigms Activation Records in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
OOP Basics Classes & Methods (c) IDMS/SQL News
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Chapter 1 Object Orientation: Objects and Classes.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
The need for Programming Languages
User-Written Functions
Overriding Method.
Object Oriented Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Testing and Debugging.
Chapter 3: Using Methods, Classes, and Objects
Class Structure 16-Nov-18.
Creating Objects in a Few Simple Steps
Class Structure 28-Nov-18.
Initializing Objects.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Chapter 4 Topics: class declarations method declarations
Class Structure 25-Feb-19.
Barb Ericson Georgia Institute of Technology Oct 2005
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Static?

Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }

Instantiation Widget w1 = new Widget(); Widget w2 = new Widget(); Widget w3 = new Widget(); There are now 4 variables w1.d w2.d w3.d Widget.s, w1.s, w2.s, w3.s are all the same!

class Widget { int d; static int s; public static void main(String args[]) { Widget w1 = new Widget(); Widget w2 = new Widget(); Widget w3 = new Widget(); w1.d = 101; w2.d = 202; w3.d = 303; Widget.s = 999; System.out.println("w1.d = " + w1.d + " w1.s = " + w1.s); System.out.println("w2.d = " + w2.d + " w2.s = " + w2.s); System.out.println("w3.d = " + w3.d + " w3.s = " + w3.s); } w1.d = 101 w1.s = 999 w2.d = 202 w2.s = 999 w3.d = 303 w3.s = 999

Why Think of static variables as living in the class Dynamic variables live in the object BUT note that the objects can refer to the static variables with no problem

Note The dynamic or instance variables were all accessed using their reference. The static (sometimes called class) variables can be accessed using a reference or the class name

A Picture class Widget { static int s; int d; } Widget Object static int s; int d; Widget Object static int s; int d; Widget Object static int s; int d; w1 w2 w3

So why static methods? A method that only refers to static variables and which could be invoked using: class.method() must be marked static

Perhaps an example?

class Widget { int serial; static int count; public Widget() { count++; serial = count; } public int getSerial() {return serial;} public int getCount() {return count;} public String toString() { return "Widget " + getSerial() + " of " + getCount(); } public static void main(String args[]) { Widget w1 = new Widget(); Widget w2 = new Widget(); System.out.println(w1); System.out.println(w2); System.out.println(getCount()); }

class Widget { int serial; static int count; public Widget() { count++; serial = count; } public int getSerial() {return serial;} public int getCount() {return count;} public String toString() { return "Widget " + getSerial() + " of " + getCount(); } public static void main(String args[]) { Widget w1 = new Widget(); Widget w2 = new Widget(); System.out.println(w1); System.out.println(w2); System.out.println(getCount()); } Can't make static reference to method int getCount() in class Widget.

class Widget { int serial; static int count; public Widget() { count++; serial = count; } public int getSerial() {return serial;} public static int getCount() {return count;} public String toString() { return "Widget " + getSerial() + " of " + getCount(); } public static void main(String args[]) { Widget w1 = new Widget(); Widget w2 = new Widget(); System.out.println(w1); System.out.println(w2); System.out.println(getCount()); }

class Widget { int serial; static int count; public Widget() { count++; serial = count; } public int getSerial() {return serial;} public static int getCount() {return count;} public String toString() { return "Widget " + getSerial() + " of " + getCount(); } public static void main(String args[]) { Widget w1 = new Widget(); Widget w2 = new Widget(); System.out.println(w1); System.out.println(w2); System.out.println(getCount()); } Why not Widget.getCount()

class Widget { int serial; static int count; public Widget() { count++; serial = count; } public static int getSerial() {return serial;} public static int getCount() {return count;} public String toString() { return "Widget " + getSerial() + " of " + getCount(); } public static void main(String args[]) { Widget w1 = new Widget(); Widget w2 = new Widget(); System.out.println(w1); System.out.println(w2); System.out.println(getCount()); } Can't make a static reference to nonstatic variable serial in class Widget.

Questions?

Debugging Java Code

Debugging Beginning programmers typically find just getting a program to compile a big challenge –Error messages are often meaningless –Error messages are often in the “wrong” place –Error messages often suggest a course of action which is dead wrong Sometimes the excitement of getting the program to compile leads the programmer to forget that the program should also work. What can go wrong –Wrong answer –Illegal operation (exceptions)

Strategies Write it right the first time. It isn’t easier to find errors later! The compiler only finds language errors not logic errors. Read the error message carefully. Sometimes they contain useful information. A null pointer exception is not mysterious! Queue q; q = null; q.enqueue(5); Null Pointer Exception

Strategies Let the computer help you find the errors In every class define a constant public final static boolean DEBUG = true; When things aren’t working add lines of code like this: if(DEBUG) { System.out.println (“method> location variable = “ + variable); } Keep adding these statements until the source of the problem is found Errors are normally caused by the computer doing what you told it to do! When the code works...change DEBUG to false Don’t remove the if(DEBUG) statements

The Main A typical Java program consists of a bunch of class files. An obvious question might be, “How does it all start up?” No matter how many classes are available, one of them must be sent to the JVM to start things off. The mechanism is simple. Any file sent to the JVM must have a public static method called main. That’s where the action starts

The Main for Debugging Starting with “main” has an additional benefit. As each class is written it can have its own main sometimes known as a debugging or test main. This test main should test all the methods in the class paying special attention to special or limiting cases. Once testing is complete: Leave the test main in the source file! It won’t have any effect and may be beneficial if later modifications are made to the class.

The Main for Debugging class Driver {... public static void main(String args[]) {... } class Stack {... public static void main(String args[]) {... } class Menu {... public static void main(String args[]) {... } class Widget {... public static void main(String args[]) {... } class Blivet {... public static void main(String args[]) {... }

Debugging Write code in small “chunks” Compile and test frequently (whenever possible) Use the test main in every class! Let’s create a linked list class that will hold Student Records. We’ll start by creating a StudentRecord class.

StudentRecord class StudentRecord { private String name; private double gpa; private int ssn; public StudentRecord(String n, double g, int s) { setName(n); setGpa(g); setSsn(s); } public void setName(String n) { name = n; } public void setGpa(double g) { gpa = gpa; } public void setSsn(int s) { ssn = s; } public String getName() { return name; } public double getGpa() { return gpa; } public int getSsn() { return ssn; } // ppp omitted to save space! // ppp omitted to save space!

toString or ! toString You may recall that System.out.println takes as a parameter a String. So why does it work if we say: System.out.println(5); Java converts it for us! Java also has a similar behavior when the argument is a reference to an object If we type: StudentRecord sr; // Make one here... System.out.println(sr); We really get System.out.println(sr.toString());

So the wise programmer... Always define a toString() method in every class. THE toString() METHOD DOES NOT PRINT ANYTHING –it merely returns a String to its caller If you omit the toString() method, Java will use a default toString() which probably won’t help you to debug. What should toString() look like?

StudentRecord toString // Precon: fields should be initialized // Purpose: return string representation // Postcon: no change to StudentRecord public String toString() { String retVal; retVal = getName() + “ “; retVal += getGpa() + “ “; retVal += getSsn(); return retVal; }

Now the test main! // Purpose: test main public static void main(String args[]) { StudentRecord sr; sr = new StudentRecord( "George Burdell", 4.0, ); System.out.println( "Should be George Burdell :" + sr); sr.setName("Bill Gates"); sr.setSsn( ); sr.setGpa(0.3); System.out.println( "Should be Bill Gates :" + sr); } } // Studentrecord

And the test! C:\demo>java StudentRecord Should be George Burdell : George Burdell Should be Bill Gates : Bill Gates C:\demo>

What's wrong? class StudentRecord { private String name; private double gpa; private int ssn; public StudentRecord(String n, double g, int s) { setName(n); setGpa(g); setSsn(s); } public void setName(String n) { name = n; } public void setGpa(double g) { gpa = gpa; } public void setSsn(int s) { ssn = s; } public String getName() { return name; } public double getGpa() { return gpa; } public int getSsn() { return ssn; } // ppp omitted to save space! // ppp omitted to save space!

Corrected class StudentRecord { private String name; private double gpa; private int ssn; public StudentRecord(String n, double g, int s) { setName(n); setGpa(g); setSsn(s); } public void setName(String n) { name = n; } public void setGpa(double g) { gpa = g; } public void setSsn(int s) { ssn = s; } public String getName() { return name; } public double getGpa() { return gpa; } public int getSsn() { return ssn; } // ppp omitted to save space! // ppp omitted to save space!

Under the Hood A detail that we need to clarify In Pseudocode, we defined a record Now we define a class In Pseudocode, we could make record on the heap Now we can make an object on the heap In Pseudocode, the new operator returned a a value we stored in a pointer variable Now the new command will return a value that we’ll store in a reference

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null;

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null; w1

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null; w1 w2

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null; w1 w2 widget object (1)

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null; w1 w2 widget object (1)

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null; w1 w2 widget object (1) widget object (2)

Example Assume we have a class Widget Widget w1; Widget w2; w1 = new Widget(); w2 = w1; w1 = new Widget(); w2 = null; w1 w2 widget object (2)