Invoking methods in String object

  1. Look carefully at the String documentation about the length() method: click here


    Look for the method length()


  2. There are two different kinds of methods in Java:

  3. Recall that Math.round(..) is a static method: click here (local copy)

    Static methods are invoked using (click here):

                        ClassName.StaticMethodName(parameter)


    Example: Math.round(4.7) Math.round(a)

  4. The length() method in class String is instance method !!!

    instance methods always operate on an object


    Example: String a = "Hello World !"; // String variable a initialized String b = "This is a test"; // String variable b initialized int len; // len is length len = a.length(); // a.length() returns length of String a len = b.length(); // b.length() returns length of String b
    *** Recall that the idea of object oriented programming is force you to manipulate the variables inside an object only through its methods (click here)

  5. Summary:

    Static Method Instance Method
      Class.MethodName(parameters)     ObjectVar.MethodName(parameters)