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

  • SEARCH
  • Home
  • 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 5931689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:43:48+00:00 2026-05-22T14:43:48+00:00

Can I override one obejct with another if they are instance of same class,

  • 0

Can I override one obejct with another if they are instance of same class, their size is the same, using sun.misc.Unsafe?

edit:
By “override” I mean to “delete” first object, ant to fill the memory with the second one. Is it possible?

  • 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-22T14:43:49+00:00Added an answer on May 22, 2026 at 2:43 pm

    By “override” I mean to “delete” first object, ant to fill the memory
    with the second one. Is it possible?

    Yes and no.

    Yes – If you allocate some memory with Unsafe and write a long, then write another long in it (for example), then yes, you have deleted the first object and filled the memory with a second object. This is similar to what you can do with ByteBuffer. Of course, long is a primitive type, so it is probably not what you mean by ‘object’.

    Java allows this, because it has the control on allocated memory.

    No – Java works with references to objects and only provides references to these objects. Moreover, it tends to move objects around in memory (i.e., for garbage collection).

    There is no way to get the ‘physical address’ and move memory content from one object address to another, if that’s what you are trying. Moreover, you can’t actually ‘delete’ the object, because it may be referenced from elsewhere in the code.

    However, there is always the possibility of having reference A point to another objectB instead of objectA with A = objectB; You can even make this atomic with Unsafe.compareAndSwapObject(...).

    Workaround – Now, let’s imagine that reference A1, A2, A3 point to the same objectA. If you want all of them to suddently point to objectB, you can’t use Unsafe.compareAndSwapObject(...), because only A1 would point to objectB, while A2 and A3 would still point to objectA. It would not be atomic.

    There is a workaround:

    public class AtomicReferenceChange {
    
        public static Object myReference = new Object();
    
        public static void changeObject(Object newObject) {
            myReference = newObject;
        }
    
        public static void main(String[] args) {
    
            System.out.println(AtomicReferenceChange.myReference);
            AtomicReferenceChange.changeObject("333");
            System.out.println(AtomicReferenceChange.myReference);
    
        }
    
    }
    

    Instead of having multiple references to the same object, you could define a public static reference and have your code use AtomicReferenceChange.myReference everywhere. If you want to change the referenced object atomically, use the static method changeObject(...).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble passing a variable defined in one class instance to another class
I am extending the MarkupSanitizerService from the sanitizer plugin. Can I just override one
How do I remove a tcl procedure? One can unset a variable, override an
Can you override method in java without using annotations? Because eclipse doesn't support it
Delphi has a standard mechanism where one object can be copied from another, paraphrased
If I have a class that implements ICustomTypeDescriptor I can override the GetProperties() method
I am having trouble passing data from one activity to another. I can easily
I have an object of class A. I want to override one of the
I know you can override delete and save methods in DJango models, but can
In Kohana 2 you can override the default error page by creating a custom

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.