Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K/B Pg135 Self Test

 
ced ron
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I'm planning to take my SCJP in 2 months time. Currently reading K/B book and need some help in the Chap 2 self test on pg135.
Q5: How many of the following are legal method declarations?
1 - protected abstract void m1();
2 - static final void m1() {}
3 - transient private native void m1() {}
4 - synchronized public final void m1() {}
5 - private native void m1();
6 - static final synchronized protected void m1() {}
Answer: E. 5 (1,2,4,5,6)
What I don't understand is why option 5 is legal? Without the curly braces, isn't it an abstract method? If it is, then it shouldn't be marked private and native. Does anyone feel there should be a pair of curly braces?
Please help. Thanks!
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never messed around with native calls in Java, but
I have seen this form before, and it is correct.
It is not an abstract method, just native methods have no
body to them.
<Dont fully understand...>
Zak Nixon
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ced
native methods are implemented in C/C++ code and then called from Java. Hence they don't have corresponding implementation in Java class. So, it allows having just decalration of native method like abstract method w/o using word 'abstract'. Also, abstract means that a subclass can define it BUT native keyword means that the implementation is there in one of the libraries created by the user and its not possible to have it implemented in Java itself. So, abstract would not make sense here if we put 'abstract' keyword in front of a native method declaration...
Hope its clear.
Regards
Maulin
 
ced ron
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Zak and Maulin for your prompt reply!
Guess I would have to be careful in assuming abstract method to be "method with no curly braces and ends with a semi-colon". Now that I take a second look at the question, it didn't ask for interface method declaration but a general method declaration else option 5 would have hit a compilation error.
ok, chapter 2 offically closed. 7 more to go!
Thanks guys! You're great!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic