Static Methods vs' NonStatic Methods - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Static Methods vs' NonStatic Methods

Description:

Where can you find out more about the data fields of a pre-written Java class? ... In a lot of the examples we have done so far, we have used the String class, ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 25
Provided by: jonc7
Category:

less

Transcript and Presenter's Notes

Title: Static Methods vs' NonStatic Methods


1
Static Methods vs.Non-Static Methods
An Introduction to Objects using the String Class
2
Review
  • When WRITING (not using) a static method, you
    need
  • modifiers (public static)
  • return type (void, int, double, char, etc)
  • method identifer (name of the method)
  • arguments/parameters (declare the variables you
    need to get from the user to work)
  • body (the stuff inside the )

3
Review
  • How do you call a static method?
  • How do you call the Math classs pow method?
  • How do you call the Math class's square root
    method?
  • Explain the difference between CALLING a method
    and WRITING a method.

4
Review
  • Where can you go to find out more about the
    methods of a pre-written Java classes?
  • What is a static class data field (or class
    variable) and how do you use it?
  • Where can you find out more about the data fields
    of a pre-written Java class?
  • What is the difference between a data field
    (class variable) and a method?

5
Static Methods (again)
A static (class) method is called by
sayingClassName.methodName(inputParameterValues
)
  • A static method is one you can call just by
    giving the name of the class it belongs to, a
    period, then the name of the method.
  • ClassName.methodName(arg1, arg2)
  • Math.pow(2,4) //24
  • static means unchanging.
  • The result you get by calling them doesnt depend
    on any circumstances except their input
    parameters

6
Static Methods
  • In the API, you can tell if a method is static or
    not by going to the method summary, and looking
    at the left-hand column of the method you want to
    check (where it also gives the return type for
    the method). If it says static, it is a static
    method.
  • If it doesnt say static, it is an object,
    non-static method

7
Objects
  • A few weeks ago, we mentioned that Java has two
    types of data types
  • primitive data types boolean, char, int, double
  • Objects user-defined data types that can be for
    anything you imagine.

8
Objects
  • Primitive data types are "basic". They consist of
    a single value, and that is it.
  • Objects are different. They consist of two
    things
  • properties the things that decide their "value"
  • behaviors things that they can do, or things
    that can be done to them. These behaviors come in
    the form of special non-static methods they can
    use called "object methods"

9
Objects
  • There are a TON of Objects that are already
    written for you to use in the Java API.
  • Today, we will start small, and use one of the
    most common objects in any programming language
    Strings.

10
Strings
  • In a lot of the examples we have done so far, we
    have used the String class, maybe even without
    realizing it.
  • When we say public static void main(String
    args) the parameter args is a String array
    (dont worry about what the are for now).
  • When we say System.out.println("afa dsgsdg
    ad")the text between the quotation marks is a
    String literal, or String value.

11
String
  • Now it is time to take a look at the String type,
    and figure out how it works
  • So what is a String?
  • String is a class (so it is not a primitive data
    type).
  • Because java.lang.String is automatically
    imported into every Java program (like the Math
    class), you can use it without having to import
    it.
  • Just like you did with primitive data types, you
    can make variables of type String that represent
    String values

12
String
  • String variable declarations and assignments
  • String name Cerny
  • final String COURSE Computer Science
  • Just like with primitive data types, when you
    declare a String variable you give the type
    (String) and identifier.
  • When you assign the variable a value, you still
    use the sign and set it equal to a value (just
    like you did with the primitive data types).

13
String literals (String values)
  • For integer literals (values), you wrote a
    number.
  • For double literals, you wrote a number with a
    decimal.
  • For boolean literals, you wrote true or false.
  • For char literals, you wrote the character value
    in single quotes, like 'a'.
  • For String literals, though, you must write the
    value in double quotes, like a.
  • 'a' is a char value
  • "a" is a String value

14
String
  • When you use the sign with numbers, it adds
    them together. When you use the sign with
    Strings, though, it concatenates them together
    (or joins the two Strings together to form a new
    String)
  • 100 200 300
  • "100" "200" "100200"

15
String Concatenation
  • Whenever you add anything to a String, it turns
    the anything into a String for the result.
  • Remember when we talked about precision, and said
    the result of an operation is always the same
    precision as the most precise number in the
    operation?
  • It works kind of the same way with Stringanytime
    you use the sign, and one of the things being
    added together is a string, the result is
    automatically a String

16
String Concatenation
  • "ab" 1 "ab1"
  • 1 "" "1"
  • "My number is " 3 "My number is 3"
  • Looking at these examples, it is a little easier
    to understand why, in our System.out.println's,
    we have been using the sign everytime we wanted
    to print out text and the value of a variable on
    the same line...the println() method takes in a
    String as its input parameter.

17
String Concatenation
  • int a 3
  • System.out.println("My variable is equal to
    "a)
  • This statement takes the String "My variable is
    equal to " and concatenates it with the value of
    variable a, which in this case is the int 3.
  • So on the screen, it will print out "My variable
    is equal to 3"

18
String an object, not a primitive data type
  • With all of the things we have done so far with
    String, it might not seem like it is all that
    different from a primitive data type like int.
  • We said that, unlike primitive data types,
    Objects like String have special non-static
    object methods that they can use.
  • Non-static methods are all the methods in the API
    that DO NOT have the word "static" next to them

19
Non-Static Object Methods
A non-static (object) method is called by
sayingvariableName.methodName(inputParameterVal
ues)
  • A non-static method is called an "object" method
    because it only works if you have an object to
    use it with.
  • To call it, you give the name of a variable
    belonging to the same class as the method, a
    period, the name of the method, and then the
    arguments, like this
  • String a "computer"
  • String b a.toUpperCase()

20
Non-Static Methods
  • We said before that System.out is the name of a
    static variable of type PrintStream.
  • Since System.out is a PrintStream variable, we
    can go to the PrintStream class and use any
    non-static methods there on it. One non-static
    method we have already used from this class is
    println().
  • System.out.println("Hello")

variable name (System.out is a static variable
of type PrintStream)
method name (println is a non-static method in
the PrintStream class)
input parameter values (the println takes in a
String, so we have to give it either a String
variable or a String literal (anything in "")
21
String's Non-Static Methods
  • There are a bunch of them in the API, but here is
    some examples of a few useful ones.
  • String example "Bob"
  • example.toUpperCase() //returns "BOB"
  • example.toLowerCase() //returns "bob"
  • example.length() //returns the int 3
  • example.replace('o', 'a') //returns "Bab"
  • example.charAt(0) /returns 'B' (the first
    character in "Bob"... Java
    always starts with 0 instead of 1. /

22
String's Non-Static Methods
  • There are a bunch of them in the API, but here is
    some examples of a few useful ones.
  • String example "Bobby"
  • example.substring(0,3) //returns "Bob"
  • example.substring(3) //returns "by"
  • example.length() //returns the int 5
  • "Pizza".toUpperCase() //returns "PIZZA"

23
Static vs Non-Static
  • We will work more with this later, but for now
    just know
  • a STATIC method is called by putting the class
    name, a period, the identifier, and the
    arguments
  • Math.sqrt(4) //returns 2
  • a NON-STATIC method is called by putting the
    variable name, a period, the identifier, and the
    arguments.
  • String x "Bob"x.toUpperCase() //returns
    "BOB"
  • For right now, we will not be writing non-static
    methods
  • We will get to that during the third six weeks.

24
StringPractice Lab
  • This next lab is a short little lab that is meant
    to
  • introduce Strings
  • introduce non-static methods
Write a Comment
User Comments (0)
About PowerShow.com