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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:52:53+00:00 2026-05-16T16:52:53+00:00

I have an odd situation where i want to be able to be able

  • 0

I have an odd situation where i want to be able to be able to persist a variable in memory.. like a global variable I can pin in the JVM.

Is this possible? I remember doing something similar in college, but can’t find it by googling. I have a logic problem that has some artificial constraints that make this the best possible solution.

EDIT 1:

I will need to update the value of the variable.

EDIT 2 :

I appreciate the responses guys. I’m a .net programmer and hadn’t used java since college. Thanks again.

  • 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-16T16:52:53+00:00Added an answer on May 16, 2026 at 4:52 pm

    Yes, using a static field:

    public class GlobalVariableHolder {
        public static int globalVariable;
    }
    

    Note, however, that this is considered a bad practice and can lead to unexpected results that are hard to debug. The way to not use a global variable is to pass it around as an argument or methods where you need it.

    If you are still sure you need this, in order to guard yourself as much as possible, use synchronization. Even better, if the variable is going to be primitive (int, long, etc), you can use AtomicInteger‘s getAndAdd() or addAndGet() method.

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

Sidebar

Related Questions

This is a bit of an odd situation. Here's what's happening. So, we have
Suppose I have this hypothetical, odd and unintuitive situation #include <iostream> struct A {
I have odd problem: After starting server I got this error: undefined local variable
This is a pretty odd situation I have here. I have used a piece
This is a really odd situation that I can't seem to work out where
I can't find this piece of information. I'm dealing with an odd situation here
I have a bit a of an odd situation happening and I can't seem
I have a situation like this. <php> <redirect the page > <exit> <javascript> <redirect
I have an odd situation in which I need to modify the position of
I have a slightly odd situation. One of my customers has an Asp.net (v2

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.