Professional Documents
Culture Documents
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Developed and taught by well-known author and developer. At public venues or onsite at your location. Contact hall@coreservlets.com for details
Object-oriented programming is an exceptionally bad idea which could only have originated in California. -- Edsger Dijkstra, 1972 Turing Award winner. 4
Basics
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
5
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Comparisons to C#
6
Object-Oriented Nomenclature
Class means a category of things
A class name can be used in Java as the type of a field or local variable or as the return type of a function (method)
Example
String s1 = "Hello"; Here, String is the class, and the variable s1 and the value "Hello" are objects (or instances of the String class)
7
Instance Variables
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
8
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Overview
Definition
Data that is stored inside an object. Instance variables can also be called data members or fields.
Syntax
public class MyClass { public SomeType field1, field2; }
In any class that also has methods, it is almost always better to declare instance variables private. We will show how and why in the next tutorial section.
Motivation
Lets an object have persistent values.
It is often said that in OOP, objects have three characteristics: state, behavior, and identity. The instance variables provide the state.
9
11
Output:
Ship1 is at (1,0). Ship2 is at (-1.41421,1.41421).
12
13
14
The new operator is required to explicitly create the object that is referenced
ClassName variableName = new ClassName();
16
Example
For example, Java has a built-in class called Point that has x and y fields
Point p = new Point(2, 3); int xSquared = p.x * p.x; int xPlusY = p.x + p.y; p.x = 7; xSquared = p.x * p.x; // Build a Point object // xSquared is 4 // xPlusY is 5 // Now xSquared is 49
In which case outside code cant access them directly. We will show later how to hook them to outside with methods.
Methods
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
18
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Overview
Definition
Functions that are defined inside a class. Methods can also be called member functions.
Syntax
public class MyClass { public ReturnType myMethod() { } }
If you want code that uses your class to access the method, make it public. If your method is called only by other methods in the same class, make it private. Make it private unless you have a specific reason to do otherwise.
Motivation
Lets an object calculate values or do operations, usually based on its current state (instance variables).
It is often said that in OOP, objects have three characteristics: state, behavior, and identity. The methods provide the behavior.
19
Methods (Continued)
public class Test2 { (In Test2.java) public static void main(String[] args) { Ship2 s1 = new Ship2(); s1.name = "Ship1"; Ship2 s2 = new Ship2(); s2.direction = 135.0; // Northwest s2.speed = 2.0; s2.name = "Ship2"; s1.move(); s2.move(); s1.printLocation(); s2.printLocation(); } }
Output:
Ship1 is at (1,0). Ship2 is at (-1.41421,1.41421).
21
22
Calling Methods
The term method means function associated with an object (I.e., member function)
The usual way that you call a method is by doing the following:
variableName.methodName(argumentsToMethod);
For example, the built-in String class has a method called toUpperCase that returns an uppercase variation of a String
This method doesnt take any arguments, so you just put empty parentheses after the function (method) name.
String s1 = "Hello"; String s2 = s1.toUpperCase(); // s2 is now "HELLO"
25
No variable name and dot is required in front of degreesToRadians since it is defined in the same class as the method that is calling it
27
Static Methods
Also class methods (vs. instance methods)
Static functions do not access any non-static methods or fields within their class and are almost like global functions in other languages
For example, the Math class has a static method called cos that expects a double precision number as an argument
So you can call Math.cos(3.5) without ever having any object (instance) of the Math class
Method Visibility
public/private distinction
A declaration of private means that outside methods cant call it only methods within the same class can
Thus, for example, the main method of the Test2 class could not have done double x = s1.degreesToRadians(2.2);
Attempting to do so would have resulted in an error at compile time
Only say public for methods that you want to guarantee your class will make available to users You are free to change or eliminate private methods without telling users of your class
In next lecture, we will see that you almost always make instance vars private and use methods to access them
30
Constructors
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
32
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Overview
Definition
Code that gets executed when new is called
Syntax
Method that exactly matches the class name and has no return type (not even void).
public class MyClass { public MyClass() { } }
Motivation
Lets you build an instance of the class, and assign values to instance variables, all in one fell swoop Lets you enforce that all instances have certain properties Lets you run side effects when class is instantiated
33
PersonTest
public class Person1Test { public static void main(String[] args) { Person1 p = new Person1(); p.firstName = "Larry"; p.lastName = "Ellison"; It took three lines of code to make a properly constructed person. It would be possible for a // doSomethingWith(p); programmer to build a person and forget to assign a first or last name. } }
34
PersonTest
public class Person2Test { public static void main(String[] args) { Person2 p = new Person2("Larry", "Page"); // doSomethingWith(p); It took one line of code to make a properly constructed person. It would not be possible for a } programmer to build a person and forget to assign a } first or last name.
35
Constructors (Continued)
public void move() { double angle = degreesToRadians(direction); x = x + speed * Math.cos(angle); y = y + speed * Math.sin(angle); } public void printLocation() { System.out.println(name + " is at (" + x + "," + y + ")."); } } public class Test3 { (In Test3.java) public static void main(String[] args) { Ship3 s1 = new Ship3(0.0, 0.0, 1.0, 0.0, "Ship1"); Ship3 s2 = new Ship3(0.0, 0.0, 2.0, 135.0, "Ship2"); s1.move(); s2.move(); s1.printLocation(); s2.printLocation(); } }
37
Output
Ship1 is at (1,0). Ship2 is at (-1.41421,1.41421).
38
39
Constructors
Constructors are special functions called when a class is created with new
Constructors are especially useful for supplying values of fields Constructors are declared through:
public ClassName(args) { ... }
Notice that the constructor name must exactly match the class name Constructors have no return type (not even void), unlike a regular method Java automatically provides a zero-argument constructor if and only if the class doesnt define its own constructor
Thats why you could say
Ship1 s1 = new Ship1();
40
41
Destructors
42
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Idea
Goal
Make a class to represent a persons first and last name
Approach: 4 iterations
Person with instance variables only
And test case
Add a constructor
And test case
PersonTest.java
public class PersonTest { public static void main(String[] args) { Person p = new Person(); p.firstName = "Larry"; p.lastName = "Ellison"; System.out.println("Person's first name: " + p.firstName); System.out.println("Person's last name: " + p.lastName); } }
45
Iteration 2: Methods
Person.java
public class Person { public String firstName, lastName; public String getFullName() { return(firstName + " " + lastName); } }
PersonTest.java
public class PersonTest { public static void main(String[] args) { Person p = new Person(); p.firstName = "Bill"; p.lastName = "Gates"; System.out.println("Person's full name: " + p.getFullName()); } }
46
Iteration 3: Constructors
Person.java
public class Person { public String firstName, lastName; public Person(String initialFirstName, String initialLastName) { firstName = initialFirstName; lastName = initialLastName; } public String getFullName() { return(firstName + " " + lastName); } }
47
PersonTest.java
public class PersonTest { public static void main(String[] args) { Person p = new Person("Larry", "Page"); System.out.println("Person's full name: " + p.getFullName()); } }
PersonTest.java
public class PersonTest { public static void main(String[] args) { Person[] people = new Person[20]; public Person(String firstName, for(int i=0; i<people.length; i++) { String lastName) { people[i] = this.firstName = firstName; new Person(NameUtils.randomFirstName(), this.lastName = lastName; NameUtils.randomLastName()); } } for(Person person: people) { public String getFullName() { System.out.println("Person's full name: " + return(firstName + " " + lastName); person.getFullName()); } } } } }
48
49
Use inheritance
Make a class (Employee) based on the Person class. Dont repeat the code from the Person class.
Next lecture
Covers all of these ideas, then shows updated code
50
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
51
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Conventions
Class names start with upper case Method names and variable names start with lower case Indent nested blocks consistently
Example class
public class Circle { public double radius; // Well make this private next lecture public Circle(double radius) { this.radius = radius; } public double getArea() { return(Math.PI*radius*radius); } }
Example usage
Circle c1 = new Circle(10.0); double area = c1.getArea();
52
Questions?
JSF 2, PrimeFaces, Java 7 or 8, HTML5, Ajax, jQuery, Hadoop, RESTful Web Services, Android, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training.
Developed and taught by well-known author and developer. At public venues or onsite at your location.