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