DOC PREVIEW
UMD CMSC 131 - Lecture Set 6: Static Methods & Variables and Exceptions

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture Set 6 Static Methods Variables and Exceptions 1 2 3 Parameter Passing Static variables and static methods Exceptions CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr Parameter Passing Parameter List Names of Parameters Primitive type parameters Reference type parameters See parameter passing example in CVS CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 1 Why Have Static Variables Methods Sometimes info needs to be shared data among all objects of a specific class type e g How many objects in a class have been created A constant that needs to be the same for all objects of that type Sometimes it is useful to have methods that are in a class that can be invoked without first creating objects of that type Static components help for these types of things CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 Declaring Static Methods and variables and constants Static methods public static void main public static void drawFlag MyGrid grid int Ccode How do we call static methods FlagMaker drawFlag grid 1 Can have static variables and constants too public static int numStudents 0 public static final int MAX ENROLLMENT 10 How do we use static variables and constants StudentRoster numStudents StudentRoster MAX ENROLLMENT CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 2 When To Use Static Methods When a method should be invocable without object creation When a method should not change instance variables A static method can only change static variables Instance variables can only be changed by nonstatic methods CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 4 Default Values Static and instance variables initialized most types to 0 char to value 0 non printable character strings are assigned to null Local variables do not have a default value and you get a error from eclipse CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 3 Calling one method from another static and non static static methods when running they ARE NOT associated with a specific instance you do NOT have a current object but you do have a current class are usually called with ClassName sMethodName if you are already in a static method since you have a class name understood as the default you can just use sMethodName non static methods when running they ARE associated with a specific instance you do have a current object are called with objectName nsMethodName if you are already in a non static method since you have a current object assumed you can just use nsMethodName to call it on that current object since that non static method must also be in the class the class name is also understood as the default so you can use sMethodName to call the static member of that class CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Exceptions Programs can generate errors 1 2 3 4 Arithmetic Divide by zero overflows Object Array Using a null reference illegal array index File and I O Nonexistent file attempt to read past the end of the file we ll see more about file I O later in course Application specific Errors particular to application e g attempt to remove a nonexistent customer from a database In Java error exception What to do when an error occurs Basically ignore it Print an error message and terminate Have the method handle it internally Handle error in the code where the problem lies as best you can Have the method pass it off to someone else to handle Return error code so that whoever called this function can handle it Modern language approach Cause exception to be thrown and caught or processed by any function up the stack trace CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 4 Exception Behavior If program generates throws exception then default behavior is Java clobbers aborts the program Stack trace is printed showing where exception was generated red and blue in Eclipse window Example public static int mpg int miles int gallons return miles gallons Throws an exception and terminates the program CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 Throwing Exceptions Yourself To throw an exception use throw command throw e e must evaluate to an exception object You can create exceptions just like other objects e g RuntimeException e new RuntimeException Uh oh RuntimeException is a class Calling new this way invokes constructor for this class RuntimeException generalizes other kinds of exceptions e g ArithmeticException CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 5 Exceptions Classes and Types Exceptions are objects Some examples from the Java class library mostly java lang ArithmeticException Used e g for divide by zero NullPointerException attempt to access an object with a null reference IndexOutOfBoundsException array or string index out of range ArrayStoreException attempting to store wrong type of object in array EmptyStackException attempt to pop an empty Stack java util IOException attempt to perform an illegal input output operation java io NumberFormatException attempt to convert an invalid string into a number e g when calling Integer parseInt RuntimeException general run time error subsumes above Exception The most generic type of exception CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 Java Exceptions in Detail Exceptions are special objects in Java They are created from classes The classes are derived inherit from a special class Throwable We will learn more about inheritance etc later Every exception object class has Exception String message Constructor taking an explanation as an argument String getMessage Method returning the embedded message of the exception void printStackTrace Method printing the call stack when the exception was thrown CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 6 Handling Exceptions Aborting program not always a good idea E mail can t lose messages E commerce must ensure correct handling of private info in case of crash Antilock braking air traffic control must recover and keep working Java includes provides the programmer with mechanisms for recovering from exceptions CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 Java Exception Terminology When an anomaly is detected during program execution the JVM throws a particular type of exception There are built in exceptions Users can also define their own more later To avoid crashing a program can catch a thrown exception if it isn t caught you see the red and blue messages stack trace An exception generated by a


View Full Document

UMD CMSC 131 - Lecture Set 6: Static Methods & Variables and Exceptions

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set 6: Static Methods & Variables and Exceptions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Set 6: Static Methods & Variables and Exceptions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Set 6: Static Methods & Variables and Exceptions and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?