Lecture 2 - Access answers (Q1, 2, 3)
package packageone;
public class A {
//data/fields
int a;
public int b;
private int c;
static public int d;
static private int e;
// methods
public void f1() {
// no op
}
private void f2() {
// no op
}
void f3() {
// no op
}
static public boolean f4() {
return false;
}
void f5() {
//Q1 : list all fields/methods in A that can be accessed here
// All good:
// int a_assign = a;
// int b_assign = b;
// int c_assign = c;
// int d_assign = d;
// int e_assign = e;
// f1();
// f2();
// f3();
// f4();
// f5();
// f6();
}
static void f6() {
// Q2: list all fields/methods in A that can be accessed here
// int a_assign = a; // illegal
// int b_assign = b; // illegal
// int c_assign = c; // illegal
// int d_assign = d;
// int e_assign = e;
// f1(); // illegal
// f2(); // illegal
// f3(); // illegal
// f4();
// f5(); // illegal
// f6();
}
}
class B {
void g() {
A objA = new A();
// Q3: list all fields/methods in objA that can be accessed here
// objA.a; // illegal
// objA.b;
// objA.c; // illegal
// objA.d; // should be accessed statically
// objA.e; // illegal
// objA.f1();
// objA.f2(); // illegal
// objA.f3();
// objA.f4(); // should be accessed statically
// objA.f5();
// objA.f6(); // should be accessed statically
}
}