200 likes | 375 Views
Java provides a String Class. The String class can be used to create String objects. String str1; //declare an object variable str1 = new String (“mary had a little lamb”);
E N D
Java provides a String Class The String class can be used to create String objects. String str1; //declare an object variable str1 = new String (“mary had a little lamb”); Strings are created SO OFTEN that Java provides a short cut for creating them (without using constructor) str1 = “mary had a little lamb”; ** Object declaration and construction (creation) can be done in one statement: String str1 = new String(“mary had a little lamb”);
Using a String Object String objects have many behaviors. One of these behaviors is called length . The length behavior of a String object provides the number of characters in the String. So: str1.length() provides the number of characters in the String object str1 And: System.out.println(“the length is” + str1.length() ); Prints the length is 22
Design a Greeter Class Greeter : String The message attribute of the Greeter class will have a String for a value ………..
Using a Greeter Class If we had a Greeter class, we could create & use Greeter objects like this: public class Application { public static void main (String[] args ) { Greeter gre1; Greeter gre2; // declare two Greeter object variables gre1 = new Greeter(“Hello, how are you!”); gre2 = new Greeter(“HOWDY!!”); System.out.println( gre1.sayGreeting() ); System.out.println( gre2.sayGreeting() ); } }
Code the Greeter Class public class Greeter { //name of the class we are defining privateString message; //each object of type Greeter will need a variable to store it’s message public String sayGreeting( ) { // this behavior is available to all classes (public) // this behavior gives back a String // this behavior requires no information to be provided return message; //statement that executes when this behavior // is executed { We still need to add the constructor method………….
the constructor method .. public Greeter(String themessage){ //accessible to other classes //requires a String,we’ll call it themessage message = themessage; //store the String which was provided // in the variable ‘message’ } }
The entire Greeter class .. public class Greeter { private String message; public Greeter(String themessage){ message = themessage; } public String sayGreeting() { return message; } }
Updated UML Greeter … the constructor needs to be provided with a String so that the message can be given an initial value … the sayGreeting method returns a String
Instance Fields (Variables) public class Greeter { ...private String message; // called instance field // or instance variable } • access specifier (such as private) • type of variable (such as String) • name of variable (such as message)
Method Definition • access specifier (such as public orprivate) public • return type (such as String or void) public String • method name (such as sayHello) public String sayHello • list of parameters (empty for sayHello) public StringsayHello ( ) • method body in { }
Parameters public Greeter(String themessage){ message = themessage; } * themessage is the name we are giving to the String value which will be provided * we will store the value which is provided in our variable message when an Greeter object is created themessage is called a parameter
Syntax Method Implementation • public class ClassName{ ... accessSpecifier returnType methodName(parameterType parameterName,...) { method body } ... }
Syntax: The return Statement return expression; or return; • Example: return message; • Purpose: To specify the value that a method returns, and exit the method immediately. The return value becomes the value of the method call expression.
The Constructor The constructor is a method used to create and object The constructor has the same name as the class (and no return type) The constructor is a method which provides initial values to the instance variables. public Greeter(String themessage){ message = themessage; }
The Greeter class cannot execute (it is not an application class, there is no main method) • The Greeter class is will be used for creating objects, and then using them. we already saw a class which USES the Greeter class….
this class can be used to test if Greeter works correctly!! public class Application { public static void main (String[] args ) { Greeter gre1; Greeter gre2; // declare two Greeter object variables gre1 = new Greeter(“Hello, how are you!”); gre2 = new Greeter(“HOWDY!!”); System.out.println( gre1.sayGreeting() ); System.out.println( gre2.sayGreeting() ); } }
Another Class for the Greeter Class public class GreeterTest { public static void main(String [] args)) { Greeter worldGreeter = new Greeter(“World”); Greeter daveGreeter = new Greeter(“Dave”); System.out.println(worldGreeter.sayHello()); System.out.println(daveGreeter.sayHello()); } }
Each object of type Greeter is an ‘instance’ of class Greeter. Each instance gets it’s own variable message, so message is called an ‘instance field or variable’.
Accessing Instance Fields • The instance field message is PRIVATE. • That means it can only be used by statements inside the Greeter class definition. • The sayGreeting method of the Greeter class can access the private instance field: • public String sayGreeting() { return message;}
The application class cannot access message public class GreeterTest{ public static void main(String[] args){ System.out.println(daveGreeter.message); // ERROR!!! } } • Encapsulation = Hiding data and providing access through methods