About Question enthuware.ocajp.i.v7.2.1069 :
Posted: Sun Feb 03, 2013 11:39 am
Answer requires for us to select 2 options but isn't option 1 right also?
Java Certification Resources and Java Discussion Forum
https://www.enthuware.com/forum/
The explanation for Option 1 says :admin wrote:Did you read the detailed explanation?
Please read all the explanation provided with the question. It also says,smearaDubha wrote:The explanation for Option 1 says :admin wrote:Did you read the detailed explanation?
"Surprisingly, it does work. Even if the class is defined in a package."
I just tried it and it does indeed work.
So this should be a valid answer also
However, for the purpose of Java Certification exam, it should be assumed that for the JVM to execute a class using the standard main method, the accessibility of the main method must be public.
During the exam, should I always expect that the main method is public? and wrong if not? Please advice.However, for the purpose of Java Certification exam, it should be assumed that for the JVM to execute a class using the standard main method, the accessibility of the main method must be public.
Code: Select all
<access modifier> <static> <final or abstract> <return type> methodName(<parameter list>)
Code: Select all
public static final void main(String[] args)
Code: Select all
final public static void main(String [ ] array)
For compilation to succeed, the only requirement is that the return type should be present just before the method name. Order of the other keywords is only by convention.The exam, however, does not try to trick you on the order
of access modifiers and final and abstract keywords.
This confuses me because I know and I've tried that final static or non-static fields/variables can be hidden by subclasses.final only means that subclasses cannot hide (in case of static methods, and static or non-static fields) or override (in case of instance methods) that method.
Code: Select all
package oca.java_basics;
class Base{
final static int i=4; // constant
final int j=5; // constant
final void im(){} // cannot override (instance)
final static void sm(){}// cannot hide (static)
// Base(){i=0; j=0;} // compiler errors
Base(){}
}
class Sub extends Base{
static int i=6;
int j=7;
// void im(){} // compiler error if we try to override
// static void sm(){} // compiler error if we try to hide
}
public class Q2_1069 {
public static void main(String[] args) {
Base b = new Sub();
System.out.println(b.i);// 4
System.out.println(b.j);// 5
Sub s = new Sub();
System.out.println(s.i);// 6
System.out.println(s.j);// 7
}
}