• 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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai all,
can anyone explain me why there is no compile time error for this code.

class Ex {

public native int getValue();

}

suppose if it is public int getValue(); the compiler will give the message
missing method body or declare it as abstract.
though there is no method body provided , the above declaration copiles fine. I know that 'native' is used for methods written in a platform dependent language. can u explain me what is happening here.
Thanks in advance.
Haripriya
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haripriya:

When we declare a method as native we don't provide any implementation for the method in java.

We use javah utility to create header and other stub file to write implementation of the native method.

So when ever we getValue() it calls the native method.

Vydhehi
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Haripriya,

native keyword is used to include methods written in other programming languages like c/c++(native code). So the actually implementation will be in a library or such which you can import it into your java program using
System.loadlibrary("xyz"). xyz being the library name.(dll)

So all you need is to declare the method that your gonna use and import the specific library and just use it as if any other method. Similar to abstract methods.

Kits
 
harry psll
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vidhehi and Kitty Dayal,
thanks a lot for your replies.
Haripriya
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I encountered the same question on one of Marcus Green's mocks

i actually tried compiling it in netBeans and it worked....my question is i did NOT have the import library statement as Kitty suggests.

why did it still work??....Kitty's answer led me to believe tht we need the System.loadlibrary("xyz") import statement
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi INXS,

You can happily compile classes containing native methods providing that they are correctly declared. The external libraries only need to be loaded if and when you want to run the code in the native method.

Hope that clarifies for you.

Jules

P.S. Are you Michael Hutchence reincarnated as a Java programmer? You're likely to get refused service by the bartender, or arrested by a sheriff, for failing to comply with the naming policy.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic