Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:26:32+00:00 2026-05-22T15:26:32+00:00

A java class does something like the following public class Foo { private final

  • 0

A java class does something like the following

public class Foo {

    private final NativeCallbackHandler handler;

    public Foo(NativeCallbackHandler handler) {
        // I've shortened this for exposition, callSomeNativeMethod 
        // really happens in a superclass that I don't own (and is 
        // part of the lib that gives me the native part)
        callSomeNativeMethod();
        this.handler = handler;
    }

    public void handleNativeCallback(Object args) {
        this.handler.callback(args);
    }

    private native int callSomeNativeMethod();
}

You can assume that the native method does something that can lead to native code calling handleNativeMethod

I have 2 related questions

  1. I believe the native code must call have a handle on this object and also call GetMethodID to get access to the method to call, is it possible for that native code to call the method before the object is fully initialised?
  2. If it can, what are the semantics of an uninitialised final field?

if 1 is yes then I expect 2 to blow up on access it and hence I imagine we’d need to make it an AtomicReference in order to access it safely without blowing up.

Note I have no control over the behaviour of the native library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:26:33+00:00Added an answer on May 22, 2026 at 3:26 pm

    Looks like it is possible. Native code does not enforce the final restriction.

    From http://java.sun.com/docs/books/jni/html/pitfalls.html#36197:

    10.9 Violating Access Control Rules

    The JNI does not enforce class, field,
    and method access control restrictions
    that can be expressed at the Java
    programming language level through the
    use of modifiers such as private and
    final. It is possible to write native
    code to access or modify fields of an
    object even though doing so at the
    Java programming language level would
    lead to an IllegalAccessException.
    JNI’s permissiveness was a conscious
    design decision, given that native
    code can access and modify any memory
    location in the heap anyway.

    Native code that bypasses
    source-language-level access checks
    may have undesirable effects on
    program execution. For example, an
    inconsistency may be created if a
    native method modifies a final field
    after a just-in-time (JIT) compiler
    has inlined accesses to the field.
    Similarly, native methods should not
    modify immutable objects such as
    fields in instances of
    java.lang.String or java.lang.Integer.
    Doing so may lead to breakage of
    invariants in the Java platform
    implementation.

    This doesn’t define behavior when you access an uninitialized final reference, but we can probably make a fairly good guess.

    Personally, I would try to avoid the problem, either by:

    • Making sure everything was initialized before the callback
    • Doing nothing during the callback until a flag was set that initialization was complete.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
(Java question) If I reference a field in an inner class, does this cause
To my knowledge, Java's File class does not support to change the file's permission
When the Java compiler autoboxes a primitive to the wrapper class, what code does
Imagine this sample java class: class A { void addListener(Listener obj); void removeListener(Listener obj);
Say I have class A with class A { final String foo() { //
Java .class files can be decompiled fairly easily. How can I protect my database
The RFC for a Java class is set of all methods that can be
I have a java class: it.eng.ancona.view.RuoliView$TabElaborazioneFattureValidazione$ElencoDettaglioElaborazioneFattureValidazione$RigaElencoDettaglioElaborazioneFattureValidazione It's so long for multiple inner class. If
I have a java class which fires custom java events. The structure of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.