About Question enthuware.ocajp.i.v8.2.1230
Moderator: admin
-
- Posts: 5
- Joined: Mon Oct 03, 2016 3:41 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1230
A course mentor has submitted the following:
The code does not compile.
The code does not compile.
- Attachments
-
- enthuware.ocajp.i.v8.2.1230_Screencast.png (145.53 KiB) Viewed 8377 times
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
What is the principle difference?
1)
Compiles fine and has no errors:
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{ }
}
2)Compiles fine but throws an error:
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.
Code: Select all
public class TestClass{
public static long main(String[] args)
{
System.out.println("Hello"); return 10L;
}
}
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
What error message do you get?
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
Compiles fine.
Error at runtime: Error: Could not find or load main class B
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)
Error at runtime: Error: Could not find or load main class B
Compiles fine.//In file B.java
import java.io.*;
class A{
public static void main() throws IOException{ }
}
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)
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.public class TestClass {
public static long main(String[] args)
{
System.out.println("Hello");
return 10L; }
}
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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:
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.
However, the question looks a bit ambiguous to me, and I answered "Hello".
Let me explain:
If you read the question as follows:
...then this code will print "Hello" because the full code would look like: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; } }
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;
}
}
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
I just wonder why that is not something the compiler complains about as it is easily foreseeable....
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
What is forseeable?
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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)"
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Mon Mar 08, 2021 4:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1230
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:
And we run the "RunnableClass" it will compile and print:
"Hello"
10
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"}));
}
}
"Hello"
10
Who is online
Users browsing this forum: No registered users and 41 guests