• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Sierra and Bates - Self Test Chapter 2 Question 5

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the self-test answers, the following is a legal method declaration:
private native void m1();
I don't see how this could be legal. It ends in a semi-colon instead of curly braces, so it can't be a concrete method, it must be abstract. Since abstract isn't one of the explicit modifiers, the method must be in an interface, not a class. But you can't have private methods in an interface! Furthermore, abtract methods can't be native.
Can someone please help me out here?
Thanks,
Ross.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'native' is an access modifiers for methods denoting that the code implementation is done in a language other than Java... So the code implementation of the method is done in say C, C++ etc..
Such methods can be defined as
native <return type> <method name>();
this is a legal method declaration
 
Ross Walker
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know what native means, but I don't see how it can be legal in this context, as I explained above.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just think of a native method as a kind of abstract method, but without the baggage that comes with abstract methods.
Now, a question: can you declare a native method static and, if yes, why.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy
Yes, this one looks confusing. The body of a method must be a semicolon if the method is abstract OR if the method is native. So the semicolon there makes it *appear* to be abstract, but it is not. It is simply a declared native method, so it must have a semicolon as the body. So it's legal, but it isn't abstract.
cheers,
Kathy
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from one of Sun's free practice exams for SCJP 1.4. The answer does not complain about this not being a complete listing, instead it refers to a bug in the code.
Given:
1. public static void main( String[] args ) {
2. class T1 extends java.lang.Thread{}
3. class T2 extends T1{}
4. class T3 implements java.lang.Runnable{}
5.
6. new T1().start();
7. new T2().start();
8. new Thread(new T3()).start();
9. System.out.println( "Executing" );
10. }
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic