lecture1_review_part2

lecture1_review_part2 - CS116 OBJECTORIENTED PROGRAMMINGII...

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

View Full Document Right Arrow Icon
CS116 OBJECT ORIENTED  PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright:  2010 Illinois Institute of  Technology- George Koutsogiannakis
Background image of page 1

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

View Full DocumentRight Arrow Icon
REVIEW Static Methods Static variables Receiving Input Command Line Receiving Input via The Scanner Wrapper Classes.
Background image of page 2
The Keyword Static  The keyword  static  can be applied to  methods and also instance variables of a  class. Applied to methods: Methods that have been declared static can be called by  using the name of the class they  belong to. Therefore we  do not need to instantiate an object of that class in order to  invoke its static methods. An example is the methods of the library class Math. If we  look at the API all the methods of this class have the word  static in their signature. That means that we can call method pow, as an example,  by :      double  a= Math.pow(a, b); 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Static applied to methods We have also seen static used in classes  where there is a main method in addition  to other methods. Rule: If we want to invoke, from the main method, another  method of the same class (that the main method  belongs to) then that method has to be declared  static. There is another way to call a non static method from the  main (assuming that both are in the same class) by creating  an object of the same class as the main and invoking the  method by  using the object. 4
Background image of page 4
Static applied to methods The previous rule applies to all methods  which are static in a class and not just  the  main method! 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Static applied to instance variables  (attributes) of a class If  an instance variable (field) is declared  static       then its value can be seen by all objects  of the same class. i.e static int id=0;          id++;     the ++ after the identifier id means that  the its value is incremented by one  every time we execute the line of code  id++ 6
Background image of page 6
Static Variables Thus if id=0;              id++ causes a new value of id=1  calling id++ again causes a new value of  id=2 And so on. If id is also declared static then every time  we increment the value of id every object  of the class sees the new value. 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Suppose that id is a static variable and that it  belongs to class Student (template class). Suppose that in StudentClient we have  instantiated the following objects: Student student1=new Student();     Student student2=new Student(); Student student3=new Student();     If the value of id=3 then all the student  objects share the same value for id (that is  3). 8
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.

This note was uploaded on 06/27/2010 for the course CS 116 taught by Professor Koutsogiannakis during the Summer '08 term at Illinois Tech.

Page1 / 35

lecture1_review_part2 - CS116 OBJECTORIENTED PROGRAMMINGII...

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