SlideShare a Scribd company logo
CSE110
Principles of Programming
with Java
Lecture 15:
Classes and Objects
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
static keyword
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
static
class
static int variable1;
public static void sayHelloAll(){
}
int variable2;
public void sayHello (){
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 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 2017 | 5
static
class
static int variable1;
public static void sayHelloAll(){
variable1 =0;
variable2 =0;
}
int variable2;
public void sayHello (){
variable1 =0;
variable2 =0;
}
Communication
Call and Create Methods
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 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 2017 | 8
Call Methods
• john.sayHello();
• john.sayHello("in French");
• int x = john.giveMeANumber();
• int result = john.addThisNumbers(2,2);
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
Create Methods
• public void sayHello() { }
• public void sayHello(String language) {}
• public int giveMeANumber(){}
• Public int addThisNumbers(int x, int y) {}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Methods
• public or private
• static (optional)
• type (void, int, double, String, etc.)
• name
• (parameters)
• { body }
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11
Example
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12
Example
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
Example
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
Reference
Pages 201 - 207
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
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.

More Related Content

PDF
201707 CSE110 Lecture 20
PDF
201707 CSE110 Lecture 19
PDF
201707 CSE110 Lecture 03
PDF
201707 CSE110 Lecture 33
PDF
201707 CSE110 Lecture 14
PDF
201707 CSE110 Lecture 01
PDF
201707 CSE110 Lecture 02
PDF
201707 CSE110 Lecture 26
201707 CSE110 Lecture 20
201707 CSE110 Lecture 19
201707 CSE110 Lecture 03
201707 CSE110 Lecture 33
201707 CSE110 Lecture 14
201707 CSE110 Lecture 01
201707 CSE110 Lecture 02
201707 CSE110 Lecture 26

Similar to 201707 CSE110 Lecture 15 (20)

PDF
201707 CSE110 Lecture 23
PDF
201707 CSE110 Lecture 21
PDF
201707 CSE110 Lecture 05
PDF
201801 CSE240 Lecture 03
PDF
201707 CSE110 Lecture 34
PDF
201707 CSE110 Lecture 09
PDF
201801 CSE240 Lecture 24
PDF
201707 CSE110 Lecture 32
PDF
201707 CSE110 Lecture 22
PDF
201506 CSE340 Lecture 18
PDF
201707 CSE110 Lecture 06
PDF
201707 CSE110 Lecture 04
PDF
201801 CSE240 Lecture 07
PDF
201707 CSE110 Lecture 17
PDF
201707 CSE110 Lecture 24
PDF
201801 CSE240 Lecture 05
PDF
201505 CSE340 Lecture 02
PDF
201801 CSE240 Lecture 22
PDF
201707 CSE110 Lecture 28
PDF
201801 CSE240 Lecture 19
201707 CSE110 Lecture 23
201707 CSE110 Lecture 21
201707 CSE110 Lecture 05
201801 CSE240 Lecture 03
201707 CSE110 Lecture 34
201707 CSE110 Lecture 09
201801 CSE240 Lecture 24
201707 CSE110 Lecture 32
201707 CSE110 Lecture 22
201506 CSE340 Lecture 18
201707 CSE110 Lecture 06
201707 CSE110 Lecture 04
201801 CSE240 Lecture 07
201707 CSE110 Lecture 17
201707 CSE110 Lecture 24
201801 CSE240 Lecture 05
201505 CSE340 Lecture 02
201801 CSE240 Lecture 22
201707 CSE110 Lecture 28
201801 CSE240 Lecture 19
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
PDF
201801 CSE240 Lecture 10
PDF
201801 CSE240 Lecture 09
PDF
201801 CSE240 Lecture 08
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 23
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11
201801 CSE240 Lecture 10
201801 CSE240 Lecture 09
201801 CSE240 Lecture 08
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

201707 CSE110 Lecture 15

  • 1. CSE110 Principles of Programming with Java Lecture 15: Classes and Objects Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 static class static int variable1; public static void sayHelloAll(){ } int variable2; public void sayHello (){ }
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 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 2017 | 5 static class static int variable1; public static void sayHelloAll(){ variable1 =0; variable2 =0; } int variable2; public void sayHello (){ variable1 =0; variable2 =0; }
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 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 2017 | 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 2017 | 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 2017 | 10 Methods • public or private • static (optional) • type (void, int, double, String, etc.) • name • (parameters) • { body }
  • 11. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11 Example
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 Example
  • 13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13 Example
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 Reference Pages 201 - 207
  • 15. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 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.