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)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    Objects Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 static

    class static int variable1; public static void sayHelloAll(){ } int variable2; public void sayHello (){ }
  3. 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.
  4. 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; }
  5. 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
  6. 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);
  7. 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) {}
  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Methods

    • public or private • static (optional) • type (void, int, double, String, etc.) • name • (parameters) • { body }
  9. CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] 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.