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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:54:59+00:00 2026-05-30T22:54:59+00:00

I know that constructors cannot be synchronized in Java. Does this mean that if

  • 0

I know that constructors cannot be synchronized in Java. Does this mean that if a constructor modifies a static variable within the class, and if constructors could be invoked from multiple threads, that the access needs to be synchronized, as shown?

public class MyClass {
  private static int count = 0;

  public MyClass() {
    synchronized(MyClass.class) {
      count++;
    }
    ...
  }
}
  • 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-30T22:55:00+00:00Added an answer on May 30, 2026 at 10:55 pm

    Absolutely – after all, it’s accessing a shared resource, potentially via many threads. I would personally just use an AtomicInteger instead.

    public class MyClass {
      private static final AtomicInteger count = new AtomicInteger();
    
      public MyClass() {
        count.incrementAndGet();
      }
    }
    

    Note that we can now make the variable final as that variable doesn’t change value, and we don’t need to synchronize any more as the whole point of the classes in java.util.concurrent.atomic is that they can be used atomically without extra synchronization.

    Even if you are in the situation where you need to synchronize, I wouldn’t use MyClass.class to do so – that’s a reference which other code might decide to synchronize on, so you can’t really reason about your code any more. I would write (again, only if AtomicInteger wasn’t good enough for some reason):

    public class MyClass {
      private static final Object countLock = new Object();
      private static int count = 0;
    
      public MyClass() {
        synchronized(countLock) {
          count++;
        }
        ...
      }
    }
    

    (In this case you’d also want to synchronize on countLock anywhere else you access count, even just for reading.)

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

Sidebar

Related Questions

Why would anyone declare a constructor protected? I know that constructors are declared private
This is probably obvious, so bear with me. YES, I KNOW THAT java.io.File has
According to the Java Language Specification , constructors cannot be marked synchronized because other
Possible Duplicate: Interface defining a constructor signature? I know that you cannot specify a
I know that I cannot pass parameters to the Activity constructor in android, but
I know that there is no return type of the constructors in C++ However,
I know that when a constructor fails, the completed member objects will be destroyed.
I know that you can't have a constructor in an interface, but here is
What i know as of now that a constructor is called when an object
I know that Qobjects are supposed to be identities not values eg you cannot

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.