week-10 - C O M P 2 2 I n t r o d u c t i o n t o C o m p u...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 Week 10 Static Artifacts and the Command-Line CONTENTS : Static variables and methods Command-line arguments Thinking Like A Programmer : Selecting when to use features in your program C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 the static modifier C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 10 3 static Members of a Class The static modifier can be applied to any class member variables as well as methods It associates a member variable or method with the class rather than an object of that class The memory space for a static variable is created as soon as the class is defined, rather than when an object of that class is instantiated. Static members of a class are usable even if no object is instantiated for that class. In that case they are accessed through the class name instead of object reference. C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 10 4 In other words class Example{ { static int x; /* x is created only once for the class. It is created as soon as the class Example is defined */ int y; int z; /* these instance variables are created each time an object is instantiated */ } class Driver { public static void main(String args){ { Example.x = 25 ; /* x is available even before any object of Example is created */ System.out.println(x = + Example.x ); Example eg1 = new Example(); Example eg2 = new Example(); eg1.x = 65; System.out.println(x = + Example.x ); /* what does it print here ?*/ } } Note that 3 physical entities exist at this point eg1: int y; int z; Class Example int x; eg2: int y; int z; C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 10 5 Static Members All objects created from the class share access to the same static variable Changing the value of a static variable in one object changes it for all other objects of that class Static methods and variables often work together C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 10 6 Try this problem Create a class called Inventory that records the product number, name, quantity and unit price of items in a store. This class constructor will initialize its fields but will also increment an integer counter called PRODUCTS that will track the number of times the class was instantiated. C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 static Methods C O...
View Full Document

This note was uploaded on 06/28/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 27

week-10 - C O M P 2 2 I n t r o d u c t i o n t o C o m p u...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online