Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1230

Posted: Thu Oct 06, 2016 10:36 pm
by corptrainer1
A course mentor has submitted the following:

The code does not compile.

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Thu Oct 06, 2016 11:41 pm
by admin
It does compile fine. Please try the code exactly as given in the question and post the compilation error message that you get.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Fri Nov 17, 2017 6:05 pm
by Sergey
What is the principle difference?
1)
Compiles fine and has no errors:

Code: Select all

//In file B.java 
import java.io.*;
class A{   
public static void main() throws IOException{ }
 }

You can have a main method that doesn't take String[] as an argument. It will not make the class executable from the command line though.
2)Compiles fine but throws an error:

Code: Select all

public class TestClass{  
 public static long main(String[] args)
  {   
   System.out.println("Hello");      return 10L;   
  } 
}

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Fri Nov 17, 2017 9:57 pm
by admin
What error message do you get?

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Sat Nov 18, 2017 10:52 am
by Sergey
Compiles fine.
Error at runtime: Error: Could not find or load main class B
//In file B.java
import java.io.*;
class A{
public static void main() throws IOException{ }
}
Compiles fine.
Error message: Error: Main method must return a value of type void in class TestClass, please
define the main method as:
public static void main(String[] args)

public class TestClass {
public static long main(String[] args)
{
System.out.println("Hello");
return 10L; }
}
Hm... if i understand it right, the general idea is when we try to use slightly different main method (or if there is no mian method at all), the code could compile but often trows an error at runtme. Right? Or, maybe it is not comparable examples.

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Sun Nov 19, 2017 12:14 am
by admin
If you want to run a class from command line then your class must have main method as:
public static void main(String[] args)

Otherwise, it will be like any other method in the class. The JVM will not be able to recognize that method as "the" main method.

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Tue Sep 11, 2018 7:33 am
by Kevin30
I spotted right away that this main-method had "long" and not "void" in its method signature, and therefore this method was not THE main method that can be run from the command line.

However, the question looks a bit ambiguous to me, and I answered "Hello".

Let me explain:
If you read the question as follows:
What will the following codeSNIPPET print when run?

Code: Select all

public class TestClass {
  public static long main(String[] args){
     System.out.println("Hello");
     return 10L;
  }
}
...then this code will print "Hello" because the full code would look like:

Code: Select all

//in file /root/com/Alpha.java
public class Alpha {
    public static void main(String[] args) {
        TestClass tc = new TestClass();
        String[] strAr = new String[2];
        tc.main(strAr);
    }
}

//in file /root/com/TestClass.java
class TestClass{
    public static long main(String[] args){
        System.out.println("Hello");		// Prints out: Hello
        return 10L;
    }
}
So, how you interpret the question depends on if you read the word 'code' or 'codesnippet'. I think on the java-exam they can also use the word 'code' even if they mean 'codesnippet', but I could be mistaken.

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Tue Sep 11, 2018 11:02 am
by admin
Oracle does ask you to assume several things such as appropriate imports as well as any supporting code. Specifically, one of the assumptions mentioned on their page says, "A code fragment is a small section of source code that is presented without its context. Assume that all necessary supporting code exists and that the supporting environment fully supports the correct compilation and execution of the code shown and its omitted environment."

However, the code given in the question is not a code snippet. It shows complete code. So there is no need to assume anything, specially when the problem clearly seems to be about "compilation and execution". This assumption is really about code fragments without any method or class declarations.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Tue Jan 12, 2021 3:44 pm
by Denyo1986
I just wonder why that is not something the compiler complains about as it is easily foreseeable....

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Tue Jan 12, 2021 9:32 pm
by admin
What is forseeable?

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Wed Jan 13, 2021 3:05 am
by Denyo1986
That there is a method that is very similar to the standard main method but doesnt fulfill all of its requirements. Would be nice to get a compiler warning highlighting the "long" in " public static long main(String[] args)"

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Wed Jan 13, 2021 3:38 am
by admin
How can the compiler know that you wanted "the" main method and not a method named main which returns a long? How will the compiler know that this class was even supposed to be executed from the command line?

Re: About Question enthuware.ocajp.i.v8.2.1230

Posted: Sun Mar 21, 2021 3:19 am
by f.motta
I think the keyword here is "run". We can have the main method just as it is. But we cant expect to run this class directly(from command line). Because the JVM will look for the main method, and this method is not appropriated since it return a Long.

But if we do this:

Code: Select all

class TestClass{
    public static long main(String[] args){
        System.out.println("Hello");
        return 10L;
    }
}

class RunnableClass{
    public static void main(String[] args) {
        System.out.println(TestClass.main(new String[]{"a"}));
    }
}
And we run the "RunnableClass" it will compile and print:
"Hello"
10