• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Native Methods.

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestClass
{
// insert declaration of a native method here
}

which of the following is correct,

1. native public void nativeMethod(int i);
2. private native void nativeMethod(int i);
3. protected int native nativeMethod();
4. public abstract native void nativeMethod(int i);
5. native int nativeMethod(int i) {}


Please give answer with explanation.
[ December 14, 2005: Message edited by: Purujit Saha ]

[ December 14, 2005: Message edited by: Purujit Saha ]
[ December 14, 2005: Message edited by: Purujit Saha ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will tell you the answer if the Java Language Specification does not.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. native public void nativeMethod(int i);
Ok
2. private native void nativeMethod(int i);
Ok
3. protected int native nativeMethod();
Not Ok. Should be protected native int nativeMethod();
4. public abstract native void nativeMethod(int i);
Not ok. Cannot be abstract and native at same time. Remove abstract.
5. native int nativeMethod(int i) {}
Not Ok. Cannot provide implementation here. It should come from native library.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so native void can have any modifier?
 
Gyan Shankar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per 4 native method cannot be declared abstract
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two simple rules:
native methods cannot be abstract.
native methods cannot have a body.
Other than that ntive methods follow the same rules as for regular methods.
Can you explain why the above two rules apply and are in place?
HTH,
Sashi
[ December 14, 2005: Message edited by: Sasikanth Malladi ]
 
Gyan Shankar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Native methods cannot be abstract because native means you will provide a implementation via some native language code but abstract means you wont so its contradictory.
Also native cannot have body because of the same explanation above.
 
reply
    Bookmark Topic Watch Topic
  • New Topic