Java Reference
In-Depth Information
public class F {
int i;
static String s;
void imethod() {
}
static void smethod() {
}
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();
}
(a)
(b)
9.18
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.19
Can you invoke an instance method or reference an instance variable from a static
method? Can you invoke a static method or reference a static variable from an
instance method? What is wrong in the following code?
1 public class C {
2 public static void main(String[] args) {
3 method1();
4 }
5
6 public void method1() {
7 method2();
8 }
9
10 public static void method2() {
11 System.out.println( "What is radius " + c.getRadius());
12 }
13
14 Circle c = new Circle();
15 }
9.8 Visibility Modifiers
Visibility modifiers can be used to specify the visibility of a class and its members.
Key
Point
You can use the public visibility modifier for classes, methods, and data fields to denote that
they can be accessed from any other classes. If no visibility modifier is used, then by default
the classes, methods, and data fields are accessible by any class in the same package. This is
known as package-private or package-access .
package-private (or
package-access)
 
 
 
Search WWH ::




Custom Search