{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-objects3 - COMP 202 COMP 202 Introduction to Computing 1...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 1 COMP 202 More on OO CONTENTS : • static revisited • this reference • class dependencies • method parameters • variable scope • method overloading
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 2 Static member variables So far: Member variables were instance variables Every object instance has its own set of these variables Member variables can also be static static variables are also called class variables Shared by all objects/instances of a class i.e., there is only one copy of this variable If one object changes the value of a static variable, all other objects see and access the new value private static int count = 0; Example use: Increase count whenever the Constructor is called Keeps track of the number of objects created;
Background image of page 2
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 3 Static methods Static methods can be executed without an object of the class instantiated Static methods can not access member variables because they always belong to a specific object/instance can access static variables and local variables All methods of Math class are static – Math.abs(-5); main method is static
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 4 Calling Methods A method calls a method m1 of the same class: Only indicate method name: m1(…); A method calls a static method m1 of another class AnotherClass: AnotherClass.m1(); A method calls a non-static m1 of another object which is referred to by the variable objVariable objVariable.m1();
Background image of page 4
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 5 The calculator as a class with only static methods import java.util.Scanner; public class CalcMain { public static void main(String[] args) { int op; double v1,v2,result; scan = new Scanner(System.in); System.out.println(“Enter 0 to exit, 1 to add, 2 to subtract”); op=scan.nextInt(); if (op==1 || op==2) { System.out.print(“Enter number: ”); v1=scan.nextDouble(); System.out.print(“Enter number: ”); v2=scan.nextDouble(); if (op==1) result = CalcMain.add(v1,v2); else result = subtract(v1,v2); System.out.println(“Result: ”+result); } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 6 The calculator as a class with only static methods public static double add(double d1,double d2) { return d1+d2; } public static double subtract(double d1,double d2) { return d1-d2; } // etc }
Background image of page 6
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 7 Class dependencies • Stand-alone Classes that do not depend or need other classes to run • Dependencies When a program calls methods of other classes and/or instantiates objects of other classes. eg. Bank uses Client class eg. use of String and Scanner classes Aggregate Classes A class that is composed from multiple other classes
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 8 Aggregate Classes A class that defines an object as one of its class variables has created a dependency between the class they are building and the
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}