• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method decleration in the main()?  RSS feed

 
sandeep atluri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run this code?

public class Mod{
public static void main(String argv[]){
}
public static native void amethod();

}

1) Error at compilation: native method cannot be static
2) Error at compilation native method must return value
3) Compilation but error at run time unless you have made code containing native amethod available
4) Compilation and execution without error.


Now the problem is when i am compiling this code,an error is generated by the name "Illegal start of expression"...
But this option is not given in the Question's answer options....

Another doubt is that whether a function can be declared in the main?
As far as i know, a function should not be defined in the main but it can be called in the main only...

So, Please.... justify this question...

Thanks..

this code is the 7th question in the following link..

http://www.jchq.net/mockexams/exam2.htm
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the problem is when i am compiling this code,an error is generated by the name "Illegal start of expression"...
But this option is not given in the Question's answer options....

That is most likely because you made a typing error. I copied and pasted your code above in a file Mod.java and it compiled without errors.
Another doubt is that whether a function can be declared in the main?
As far as i know, a function should not be defined in the main but it can be called in the main only...

What do you mean by "in the main" - inside the main(...) method? No, you cannot define methods inside other methods, but that's not what your code is doing. There is a closing } before the declaration of amethod(), so it's not inside the main(...) method.
[ May 02, 2007: Message edited by: Jesper Young ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!