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 7948639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:46:50+00:00 2026-06-04T01:46:50+00:00

I have a Long variable say, private Long m_prevPacketRecvdTime = null; now I have

  • 0

I have a Long variable say,

private Long m_prevPacketRecvdTime = null;

now I have a setter/getter pair for it,

    public void setM_prevPacketRecvdTime() {
        if (this.m_prevPacketRecvdTime == null) {
            this.m_prevPacketRecvdTime = new Long(System.currentTimeMillis());
        } else {
            // Why to create a new Object every time
                    //Why I can't just set the new value
                    //Why use new operator here instead of an Assignment or set method call
        }
    }

Instead of traditional setter method I thought to improve it, by 1. passing long instead of Long, 2. Creating a new Long only once and rest of the time just setting the new value to existing Long object. But to my disbelief there is no such method in Long.

Do we really need to create a new Long object every time we want to change its value, How can we set a new long value to existing Long object (just opposite of longvalue()) ?

More Info
While Searching for the answer I also came across AtomicLong, which has similar functionalities, But I am not sure whether that can be useful for me or not. Because they clearly say AtomicLong “cannot be used as a replacement for a Long.“

  • 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-06-04T01:46:52+00:00Added an answer on June 4, 2026 at 1:46 am

    You cannot, and it’s deliberate. Immutable objects have many advantages — thread safety, ease of use, security and predictability among other things. See Effective Java item 15 for additional reasons, but the wrapper classes in Java are all immutable.

    That said, you should use Long.valueOf(long) (or autoboxing) instead of new Long, because Long.valueOf knows to cache commonly used values.

    Allocation is relatively cheap in Java. Don’t worry too much about it. If you can produce hard numbers to prove that switching to your own custom mutable wrapper type is worth the additional code complexity, then do that, but the traditional immutable wrappers are perfectly serviceable.

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

Sidebar

Related Questions

i have a long string variable and i want to search in it for
I have the following code in specman: var x := some.very.long.path.to.a.variable.in.another.struct; while (x ==
Lets say i have a variable that contains the number of search engine names
This question is long winded because I have been updating the question over a
I have a really long winded switch case statement like this switch(x){ case a:
Hey there, I have a custom text inside a variable: $myvar = 'this is
Say I have a Java program such: //case1 Long first = 1; Long second
For example, say we have a union typedef union { unsigned long U32; float
Let's say I have this HTML: <select id='list'> <option value='1'>First</option> <option value='2'>Second</option> <option value='3'>Third
let's say I have a string called str , I dont know how long

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.