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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:45:43+00:00 2026-06-04T05:45:43+00:00

I knew that Static variables are created and initialized only one time I.e when

  • 0

I knew that Static variables are created and initialized only one time I.e when the class is loaded and not per object.

In the code given below what I am trying to do is: I declared an int variable “a” as static in class A and assigned it a value of 5. When the main is executed I changed its value to 6 and made the program to loop infinitely. So currently “a” value is 6.

Now what I tried to do is I tried to access this variable from other class class B when class A is still looping and I expected that 6 should be printed when I ran class B (because by the time I ran class B “a” value is changed to 6) but surprisingly it still gave me 5.

Why is this happening even though I declared “a” as static? Unable to figure out what’s wrong with this approach.

class A{
    static int a=5;

    public static void main(String args[]){
        System.out.println(A.a+"");
        a=6;
        while(true){
        }
    }
}

class B{    
    public static void main(String args[]){
        System.out.println(A.a+"");
    }
}
  • 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-04T05:45:45+00:00Added an answer on June 4, 2026 at 5:45 am

    The problem you are having is that you are running two different JVM processes. What you should be doing is running each of your “main” methods in its own thread. This way they share (and therefore can modify) the same memory. Here is your example modified to use threads:

    public class Main{
        public static int v = 5;
    
        public static void main(String[] args) throws Exception{
            new Thread(new A()).start();
            Thread.sleep(5000);
            new Thread(new B()).start();
        }
    }
    
    class A implements Runnable{
        public void run(){
            System.out.println(Main.v);
            Main.v = 6;
        }
    }
    
    class B implements Runnable{
        public void run(){
            System.out.println(Main.v);
        }
    }
    

    Side note: While this example does demonstrate what is expected from the question, there is still a lot of things wrong in terms of writing a correct Java program that uses multiple threads. Do your reasearch.

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

Sidebar

Related Questions

When I wanted to get Android source code, I knew that I have to
I did not knew that this simple thing would be slightly complicated. I have
Same problem was discussed earlier and I also knew that nothing, not also a
I have some public variables that are defined as follows: public class FieldsToMonitor {
I have the following code class Program { static void Main(string[] args) { List<A>
hey guys. i was wandering if anyone knew how to create code so that
I knew that java.util.Calendar class method getInstance() returns a GregorianCalendar but how could i
I have some code that is really slow. I knew it would be and
I knew that you can develop customised code to generate pdf report via SQL
facebook has the facebook credit and I knew that they are testing on pay

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.