Section 9.7 Check Point Questions3 questions
9.7.1
Suppose that the class F is defined in (a). Let f be an instance of F.
Which of the statements in (b) are correct?
(a) public class F { int i; static String s; void imethod() { } static void smethod() { } } (b) System.out.println(f.i); System.out.println(f.s); f.imethod(); f.smethod(); System.out.println(F.i); System.out.println(F.s); F.imethod(); F.smethod();
9.7.2
Add the static keyword in the place of ? if appropriate.
public class Test { int count; public ? void main(String[] args) { ... } public ? int getCount() { return count; } public ? int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result *= i; return result; } }
9.7.3
1. Can you invoke an instance method or reference an instance
variable from a static method?
2. Can you invoke a static method or reference a static variable from an instance method?
3. What is wrong in the following code?
2. Can you invoke a static method or reference a static variable from an instance method?
3. What is wrong in the following code?
1 public class C { 2 Circle c = new Circle(); 3 4 public static void main(String[] args) { 5 method1(); 6 } 7 8 public void method1() { 9 method2(); 10 } 11 12 public static void method2() { 13 System.out.println("What is radius " + c.getRadius()); 14 } 15 }