Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are native Java methods equivalent to static Java methods?

I am rewriting some native methods as regular Java methods.

Are native methods effectively static? Or is there ever a case where they have an implicit 'this' parameter?

Thanks!

like image 364
user1324109 Avatar asked Mar 06 '13 17:03

user1324109


People also ask

What is the difference between Java method and native method?

A Java method is generically a code block with a name that you can write using plain java. A native method is a method that is linked to a native library.

What is native Java method?

Native methods are Java™ methods that start in a language other than Java. Native methods can access system-specific functions and APIs that are not available directly in Java. The use of native methods limits the portability of an application, because it involves system-specific code.

What is the alternative to static in Java?

You can use the full power of inheritance and overriding since your methods are no longer static. You can use the constructor to do any initialisation, including associating SQL with the table (SQL that your methods can use later). This should make all your problems above go away, or at least get much simpler.

Why should we avoid static methods in Java?

That's because static methods can't access the object's attributes. Static methods aren't part of the object, so they don't have access to anything that belongs to the object.


1 Answers

Native methods can be static or non-static, just like regular Java methods.

Non-static native methods receive this reference, static ones receive a reference to containg class instead.

From JNI Specification:

Native Method Arguments

The JNI interface pointer is the first argument to native methods. The JNI interface pointer is of type JNIEnv. The second argument differs depending on whether the native method is static or nonstatic. The second argument to a nonstatic native method is a reference to the object. The second argument to a static native method is a reference to its Java class.

like image 51
axtavt Avatar answered Oct 03 '22 07:10

axtavt