Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 16

CSE110 Lecture 16

Principles of Programming with Java
Classes and Objects
(202005)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

June 09, 2017
Tweet

Transcript

  1. CSE110 Principles of Programming with Java Lecture 16: Classes and

    Objects Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. static keyword

  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 static

    class static int variable1; public static void sayHelloAll(){ } int variable2; public void sayHello (){ }
  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 static

    • Non-static methods can use static and non-static variables • static methods can only use static variables. • static variable = class variable • Non-static variable = instance/object variables/attributes.
  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 static

    class static int variable1; public static void sayHelloAll(){ variable1 =0; variable2 =0; } int variable2; public void sayHello (){ variable1 =0; variable2 =0; }
  6. Communication Call and Create Methods

  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Object-Oriented

    Paradigm public class Student { String name; int finalGrade; public void sayHello(){ // print name } public void drawSomething(){ // draw } } public class MyProgram { public static void main(String [] arg) { Student john = new Student(); john.name = "John Doe"; john.sayHello(); jane.name = "Jane Doe"; jane.sayHello(); } } john jane
  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Call

    Methods • john.sayHello(); • john.sayHello("in French"); • int x = john.giveMeANumber(); • int result = john.addThisNumbers(2,2);
  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Create

    Methods • public void sayHello() { } • public void sayHello(String language) {} • public int giveMeANumber(){} • public int addThisNumbers(int x, int y) {}
  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Methods

    • public or private • static (optional) • type (void, int, double, String, etc.) • name • (parameters) • { body }
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Example

  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Example

  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Example

  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Reference

    Pages 201 - 207
  15. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2020

    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.