csc 201-lecture 11 final

Upload: pavanil

Post on 30-May-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 CSC 201-Lecture 11 Final

    1/19

    CSC 201

    Lecture - 11

  • 8/14/2019 CSC 201-Lecture 11 Final

    2/19

    Static and Instance Methods

    public class MainClass {

    public void sort()

    {

    }

    public static void testMethod()

    {}

    public static void main(String[] args)

    {

    MainClass m = new MainClass();

    m.sort();testMethod();

    }

    Instance method

    Static method

    Creating an object m

  • 8/14/2019 CSC 201-Lecture 11 Final

    3/19

    Static and Instance methods

    Instance Methods: are associated with an object and useinstance variables of that object.

    Static methods: They use no instance variables of anyobject of the class they are defined in. Static methods

    take all data from parameters and perform computation. A non static method cannot be referenced from a static

    context, whereas static method can be referenced from astatic context.

    Static methods or static fields are always classmembers.

    Non-static methods and fields are always instancemembers.

  • 8/14/2019 CSC 201-Lecture 11 Final

    4/19

    Another Example

    class staticcheck{

    public static void staticMethod() {}

    public void nonStaticMethod() {}

    }

    public class MainClass {

    public static void main(String[] args){

    staticcheck s = new staticcheck();

    staticcheck.staticMethod();

    s.nonStaticMethod();

    }

    }

  • 8/14/2019 CSC 201-Lecture 11 Final

    5/19

    Example

    public class accessStaticMethod {

    public static void staticMethod()

    {

    System.out.println(Here is static method access);

    }

    public void nonStaticMethod()

    {System.out.println(Cannot access non-static method:);

    }

    public static void main(String[] args)

    {

    staticMethod();

    nonStaticMethod();

    }

    }

    What happens when you write nonStaticMethod() ? How to overcome this problem?

  • 8/14/2019 CSC 201-Lecture 11 Final

    6/19

    Static Variables

    Usage: To define constants. Ex: Math.PI

    Usually programming languages have

    global variables.

    In case of Java, we can define static class

    variable in a class. A static variable like a

    static method is not attached to an object.

    Each time you call the instance a new

    value of the variable is provided.

  • 8/14/2019 CSC 201-Lecture 11 Final

    7/19

    Example-Static Variable

    public class staticVariableDemo

    {

    static int num;

    staticVariableDemo()

    {

    num++;}

    public static void main(String[] args)

    {

    staticVariableDemo inst1 = new staticVariableDemo();

    System.out.println(Valueof num is:+inst1.num);

    staticVariableDemo inst2 = new staticVariableDemo();System.out.println(Value of num with inst1 is:+inst1.num);

    System.out.println(Value of num with inst2 is:+inst2.num);

    }

    }

  • 8/14/2019 CSC 201-Lecture 11 Final

    8/19

    Agenda

    What is an exception?

    What happens when an exception occurs?

    How to catch these exceptions? Try catch block

    Rules in Exception Handling

    Sample programs Exception Handling

  • 8/14/2019 CSC 201-Lecture 11 Final

    9/19

    Errors in Java

    There are two kinds of errors: Compile-timeerrors and Run-time errors.

    Compile-Time errors: Syntax errors.

    Ex: int num = 1Error: No ;.

    Run time errors: An error that occurs duringruntime.

    Ex: Divide By Zero errors, Accessing theelements of an array beyond its range, File notfound etc.

  • 8/14/2019 CSC 201-Lecture 11 Final

    10/19

    Exception example program

    public class Main

    {

    public static void main(String[] args)

    {

    System.out.println("I want to show you a Runtimeerror");

    int num1 = 3, num2 = 0;

    int result = num1 / num2;

    System.out.println(result);

    }

    }

    What is the output?

  • 8/14/2019 CSC 201-Lecture 11 Final

    11/19

    Sample Program

    What happens in the following scenario?

    public static void main(String[] args)

    {int [] a= {1,2,3,4,5};

    for(int i =0; i < 6; i++)

    System.out.println(Elements of array a are +a[i]);

    }

    Array Index out of bounds?

  • 8/14/2019 CSC 201-Lecture 11 Final

    12/19

    Default Exception Handling

    Error messages displayed :

    I want to show you a Runtime errorException in thread "main"

    java.lang.ArithmeticException: / by zero

    atDivideNumbers.Main.main(Main.java:15)

  • 8/14/2019 CSC 201-Lecture 11 Final

    13/19

    Default Exception Handling

    Error message displayed for our Array program:

    Elements of an array are:

    1

    2

    3

    4

    5

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

    at ArrayCheck.Main.main(Main.java:16)

  • 8/14/2019 CSC 201-Lecture 11 Final

    14/19

    Exception

    An exception is an event that occurs

    during the execution of a program that

    disrupts the normal flow of instructions.

  • 8/14/2019 CSC 201-Lecture 11 Final

    15/19

    Default Exception Handler

    Provided by Java runtime.

    Prints out exception description.

    Causes the program to terminate

  • 8/14/2019 CSC 201-Lecture 11 Final

    16/19

    Catching and Handling Exceptions

    Three exception handler components : the Try, catchblocks to write an exception handler.

    Try Block :

    The first step in constructing an exception handler is to

    enclose the code that might throw an exception within atry block.try {

    // Lines of code which may possibly

    // throw exception.

    }

    catch (Exception exceptionobject)

    {

    //Catches the error and explain the cause of the error.

    }.

  • 8/14/2019 CSC 201-Lecture 11 Final

    17/19

    Example

    int[] a = new int[5];

    try {

    System.out.println(a[-1]);

    }catch(Exception e) {}

    If an exception occurs within a try block, that

    exception is handled by an exception handlerassociated with it. To associate an exceptionhandler with a try block, you must put a catchblock after it.

  • 8/14/2019 CSC 201-Lecture 11 Final

    18/19

    Catch blocks

    try {

    ______

    ______

    } catch (ExceptionType name) {

    } catch (ExceptionType name) {}

    Each catch block is an exception handler and handlesthe type of exception indicated by its argument.

    The catch block contains the code that is executed if andwhen the exception handler is invoked.

    A single try block must have at least 1 or more catchblocks.

  • 8/14/2019 CSC 201-Lecture 11 Final

    19/19

    Example try catch blocks

    try {

    }catch (FileNotFoundException e)

    { System.out.println(File not foundexception:);

    } catch( IOException e)

    { System.out.println(Caught IoException:);

    }