CSC240 Computer Science III Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa.edu 11/12/2018
Table of Contents Introduction Declaration and call Access Scope 11/12/2018
Introduction Why? Why loop Why array Why method Why object (the host of instance method)? 11/12/2018
Every thing under the control inside of object (which must be declared in the class, as a general template to cover all possible changes/statuses) 11/12/2018
state: String name, breed int age Dog class state: String name, breed int age behavior: writeOutput ( ) getAgeInHumanYears ( ) Dog object balto state: “Balto” (name) 8 (age) “Siberian Husky” (breed) behavior: getAgeInHumanYears ( ) writeOutput ( ) Dog object scooby state: “Balto” (name) 8 (age) “Siberian Husky” (breed) behavior: getAgeInHumanYears ( ) writeOutput ( )
Declaration and Call No more static How to use instance method m()? public class A { public void m() { … } 11/12/2018
Access Where is possible to call? Call at each different scenario 11/12/2018
From another instance method in the same or different class public class A { public void m() { … } public void m2() { m(); public class B { public void m() { … } public void m3() { m() A a; //a.m is not supported A b = new A (); b.m(); 11/12/2018
From static method such as void main public class A { public void m() { … } public static void m2() { A a = new A(); a.m(); 11/12/2018
Call of a static method by instance methods, for a comparison public class A { public static void m() { … } public void m2() { m(); public class B { public void m() { … } public void m3() { m() A.m(); 11/12/2018
Self-test public class A { public void m1(){ System.out.println("A's m1"); m2(); } public static void m2(){ System.out.println("A's static m2"); } } public class B { public void m1(){ A a = new A(); a.m1(); A.m2(); } public static void m2(){ System.out.println("B's static m2"); B b2 = new B(); b2.m1(); } public static void main(String args[]){ A a = new A(); B b = new B(); b.m1(); B.m2(); } } A's m1 A's static m2 B's static m2 11/12/2018
Constructor Name No return Not static Related to the use of new Overloading 11/12/2018
Mutator Accessor toString Argument and parameter Type (compatible & overloading) Primitive type and reference type (array & object) Accessor Return toString 11/12/2018
Constructor public class A { public A() { … } public A(int x) { public class B { public void m3() { A a = new A(); A b = new A(2); } 11/12/2018
Mutator and accessor public class A { public A() { … } public void a() { public void A() { public class B { public void m3() { A a = new A(); a.a(); a.A(); // but very confused! } 11/12/2018
Note: Every class has a toString method. When an object is printed or concatenated with a String, Java calls the object's toString method. System.out.println("p1 is " + p1); is equivalent to: System.out.println("p1 is " + p1.toString()); Note: Every class has a toString method. 11/12/2018
toString public class A { private int x = 2; public A() { … } public String toString () { // no parameter! return “a” + x; public class B { public void m3() { A a = new A(); System.out.println(a); } 11/12/2018
Scope Attribute (property, data field, etc) Parameter What is “this” 11/12/2018
Attribute what is this public class A { private int x; public A(int y) { x = y; } public void m(int x) { // int x; this.x = x; public String toString(){ return x+””; public class B { public void m3() { A a = new A(2); a.m(3); System.out.println(a); } 11/12/2018
Parameter public class A { public int x; public A(int y) { x = y; } public void n() { // another method // other than m public void m(A a) { // what attribute can be used: x vs. a.x // What refers, by the call a.m(b): a.x vs. b.x // What method can be used: n(); vs. a.n(); // What they refer, by a.m(b): a.n() and b.n() public class B { public void m3() { A a = new A(2); A b = new A(3); a.m(b); } 11/12/2018
What is local a, is it different from that in a.m(b)? Parameter public class A { private int x; public A(int y) { x = y; } public void n() { // another method // other than m public int getX(){ return x; public void m(A a) { // How to access a.x and b.x inside m // upon the call a.m(b): x vs. a.getx() // Call methods a.n and b.n insider m // upon the call a.m(b) n(); vs. a.n(); public class B { public void m3() { A a = new A(2); A b = new A(3); a.m(b); } What is local a, is it different from that in a.m(b)? 11/12/2018
Why b. x is used in slide 20 but b. getx(), i. e Why b.x is used in slide 20 but b.getx(), i.e., a method is used in slide 21? Public vs. private 11/12/2018