In this exercise, you will learn how to nvoke both static and non-static (instance) methods of a class. Please note that a static method of a class is called in the following form:
*
* Example:
o Integer.parseInt("25"); // Convert "25" into int type
while an instance method of class is called only through an object instance as following.
*
* Example
o String str1 = new String("Hello"); // Create an object instance of String class
o char x = str1.charAt(0); // Call an instance method charAt() of String class through object instance
Steps to follow:
1. Write StaticAndInstanceMethods.java. (You are welcome to do this work using either command line tools or NetBeans. The instruction here is given using command line tools. In general, using NetBeans is highly recommended.)
* cd \myjavaprograms
* jedit StaticAndInstanceMethods.java
public class StaticAndInstanceMethods{
public static void main( String[] args ){
// Create two instances of String class
String strInstance1 = new String ("I am object instance of a String class");
String strInstance2 = "Live your passion!";
// Invoke an instance method charAt() through an object instance of String class
char x = strInstance1.charAt(2);
char y = strInstance2.charAt(1);
char z = strInstance2.charAt(0);
System.out.println("The 3rd char of strInstance1 = " + x);
System.out.println("The 2nd char of strInstance2 = " + y);
System.out.println("The 1st char of strInstance2 = " + z);
// Invoke an instance method equalsIgnoreCase(..) method
boolean b = strInstance1.equalsIgnoreCase(strInstance2);
String strInstance3 = b? "Yes":"No";
System.out.println("Do strInstance1 and strInstance2 have same string ignoring case? " + strInstance3);
// Invoke a static-method, valueOf (int i), of the String class
int i = 23;
String strInstance4 = String.valueOf(i);
System.out.println("value of strInstance4 = " + strInstance4);
// You already have used parseInt() static method of the Integer class in
// previous exercises.
String strInstance5 = new String("34"); // Create an object instance of String class
int ii = Integer.parseInt(strInstance5);
System.out.println("value of ii = " + ii);
}
}
Code 9.2: StaticAndInstanceMethods.java
2. Compile and run the code
* javac StaticAndInstanceMethods.java
* java -classpath . StaticAndInstanceMethods
3. Verify the result is as following
* C:\myjavaprograms>java -classpath . StaticAndInstanceMethods
The 3rd char of strInstance1 = a
The 2nd char of strInstance2 = i
The 1st char of strInstance2 = L
Do strInstance1 and strInstance2 have same string ignoring case? No
value of strInstance4 = 23
value of ii = 34
Homework:
1. Write StaticAndInstanceMethods2.java as following, and compile and see what compile errors you get. Fix any compile errors.
public class StaticAndInstanceMethods2{
public static void main( String[] args ){
// Create an instance of a String class by using a keyword "new".
// For example, in order to create an object instance of a String class,
// you will do the following
String strInstance1 = new String ("I am object instance of a String class");
// The following code will generate a compile error since you are trying to
// invoke a instance method through a class. Fix this compile error.
char x = String.charAt(2);
}
}
Code 9.2.b: StaticAndInstanceMethods2.java
No comments:
Post a Comment