What Would Be The Result of Attempting To Compile and Run The Following Program?
What Would Be The Result of Attempting To Compile and Run The Following Program?
What Would Be The Result of Attempting To Compile and Run The Following Program?
program?
class MyClass {
static MyClass ref;
String[] arguments;
public static void main(String[] args) {
ref = new MyClass();
ref.func(args);
}
public void func(String[] args) {
ref.arguments = args;
}
}
The program will fail to compile, since the static method main() cannot have a call to
the non-static method func().
The program will fail to compile, since the non-static method func() cannot access
the static variable ref.
The program will fail to compile, since the argument args passed to the static
method main() cannot be passed on to the non-static method func().
The program will compile and run successfully.
Topic: Access Modifiers
package com.test.work;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
class B {
// (1)
// (2)
}
void f() {}
void h() {}
// void k() { i++; }
// (3)
// (4)
int m;
}
// (1)
// (2)
// (3)
// (4)