CS 201 OBJECT ORIENTED PROGRAMMING I LECTURE 7
CS 201 OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology. George Koutsogiannakis 1
PREVIOUS TOPICS • Using the Scanner class to read user input from the keyboard. • Wrapper classes. • Autoboxing / Unboxing. 2
NEW TOPICS • Static Variables and their usage. • Using Command Line for user input. • Using the JOption. Pane for user input. 3
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); 4
Keyword 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. 5
Static applied to methods • The previous rule applies to all methods which are static in a class and not just the main method! 6
Static applied to methods • We Know by now that the main method is a static method i. e. public static void main(String[] args) {…. . } • RULE: – You can only call static methods from within a static method. – Non static methods can be invoked from within a static method by using an object of the class that the static method belongs to. 7
Static applied to methods- Calling other methods from a static method • Example public class Some. Class { public static void main(String[] args) { //some code here //next we want to make a call to the method Another. Method int x=Another. Method(); } public int Another. Method() { int var=0; //some code sets the value of var return var } } THE COMPILER WILL GIVE US AN ERROR MESSAGE BECAUSE WE ARE TRYING TO CALL A NON STATIC METHOD FROM WITHIN A STATIC METHOD 8
Static applied to methods- Calling other methods from a static method • HOW DO WE FIX THE ERROR? • TWO OPTIONS – Declare the method Another. Method in our example to be static public static int Another. Method() { //code } – The other option is to use an object of class Some. Class to invoke the method Another. Method The code for this choice is shown on the next slide. 9
Static applied to methods- Calling other methods from a static method public class Some. Class { public static void main(String[] args) { //some code here //next we want to make a call to the method Another. Method Some. Class sc = new Some. Class(); int x= sc. Another. Method(); } public int Another. Method() { int var=0; //some code sets the value of var return var } } 10
Keyword 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++ 11
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. 12
Static Variables • Suppose that id is a static variable and that it belongs to class Student (template class). • Suppose that in Student. Client we have instantiated the following objects: Student student 1=new Student(); Student student 2=new Student(); Student student 3=new Student(); If the value of id=3 then all the student objects share the same value for id (that is 3). 13
Example of static variable public class Student { static int id=0; int current. ID=0; String student. Name=" "; public Student() { id++; current. ID=id; student. Name=“John Doe”; } public Student ( String sn) { Studentname=sn; } followed by accessor /mutator/to. String() methods 14
Example of static variable • We notice that in class Student – Every time the constructor is called the value of id is advanced by one with respect to its previous value. – Once the value of id is advanced its new value is assigned to a non static variable : current. ID. – That is because the non static variables have values that are dedicated to each object and their values are not shared by all objects of the class. 15
Example of static variable public class Student. Client { public static void main(String [] args) { Student st 1=new Student() // value of id=1 now for st 1. Every time we call the constructor of the class Student id advances by 1 //Also, The value of id is passed to Student st 2=new Student() // value of id=2 now for both st 1 and st 2, // current. ID=1 for st 1 but // current. ID=2 for st 2. Student st 3=new Student() // value of id=3 for st 1, st 2, st 3 // current. ID=1 for st 1 // current. ID=2 for st 2 // current. ID=3 for st 3 current. ID =1 16
Example of static variable • Therefore we could use the static variable id to track how many objects we have created for that class (i. e. the Student class). • Notice that the new value of the static variable id is shared amongst all objects of the class. 17
Example of static variable • The value of id is the same for each object student 3 is created: Student 1 id-=3 current. ID=1 Student 2 Student 3 id-=3 current. ID=2 current. ID=3 Since id is static all 3 objects see its new value when it changes (if a 4 th Student was to be created all 4 objects will have id=4). The variable current. ID is not static therfore each object sees the value of current. ID assigned for that object alone. 18
Command Line Input • We have used the scanner object to obtain input from a user via the keyboard. • Another way of obtaining input for a program is from the DOS pane when we use the interpret command. • This is called “command line “ input. • i. e >java Student. Client arg 1 arg 2 arg 3 Where arg 1 arg 2 arg 3 will be read as individual Strings by the main method of the program . • This approach is only good as long as the class has a main method (can not be used to input data into a template class). 19
Command Line Input • Suppose that we have the class Student. Client and that it expects 3 inputs (called arguments) from command line • >java Student. Client arg 1 arg 2 arg 3 • public class Student public static void main(String [] args) { String a=args[0]; String b=args[1]; String c=args[2]; 20
Command Line Input • The Strings a , b , c now have the values that the user entered: i. e. >java Student. Client Joe , 20, 4. 65 thus: a has the value “Joe” b has the value “ 20 “as a String c has the value “ 4. 65” as a String 21
Command Line Input • If we wanted to do arithmetic operations with the String versions of values 20 and 4. 65 then we have to parse the String verions by using the appropriate wrapper class • i. e int x= Integer. parse. Int(b); results in the int version of the String 20 (int x=20) double y=Double. parse. Double( c ); results in the double version of the String 4. 65 (double y=4. 65) 22
JOption. Pane class • The JOption. Pane is a pre defined class that has methods that allow the creation of a graphical message box. • The user types into the box and the program captures what the user typed as a String data type. • We can change the data type from a String to another type by using the proper Wrapper class and by parsing. • We can respond back to the user by displaying another graphical box that has the output of the program. 23
JOption. Pane static Methods Return Method name and argument list value String show. Input. Dialog( Component parent, Object prompt ) void pops up an input dialog box, where prompt asks the user for input. show. Message. Dialog( Component parent, Object message ) pops up an output dialog box with message displayed See Examples 3. 19 Dialog. Box. Demo 1. java
JOption. Pane • Notice that since the methods are static we can call them (use them) by invoking them with the name of the class they belong to and the dot operator i. e String str=JOption. Pane. show. Input. Dialog(null, “Information to User”, “Please enter a whole number”, JOption. Pane. INFORMATION_MESSAGE) 25
Example of JOption. Pane import javax. swing. JOption. Pane; public class Test. JOption. Pane { public static void main(String[] args) { String str=JOption. Pane. show. Input. Dialog(null, "Input from User", "Enter a integer", JOption. Pane. INFORMATION_MESSAGE); int x=Integer. parse. Int(str); JOption. Pane. show. Message. Dialog(null, "The value you entered was"+x); System. exit(0); } } 26
Example of JOption. Pane • Notice that the package javax. swing needs to be imported. 27
Study Guide • Chapter 7 section 7. 11 • Chapter 3 section 3. 16 28
- Slides: 28