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

The Archive Base Latest Questions

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

I hope this is going to be enough information, so here it goes. If

  • 0

I hope this is going to be enough information, so here it goes. If you need more info, lemme know in the comments.

I have a class that has two inner classes. The inner classes each have two methods that call a method in the outer class. So, it looks like this:

public OuterClass {
    private boolean outerMethodHasBeenCalled = false;

    private void outerMethod() {
        if(!outerMethodHasBeenCalled) {
            // do stuff
        }

        outerMethodHasBeenCalled = true;
    }

    private FirstInnerClass {
        public void someMethod() {
            outerMethod();
        }
    }

    private SecondInnerClass {
        public void someOtherMethod() {
            outerMethod();
        }
    }
}

It’s important to note that:

  • This is for an Android app. Instances of FirstInnerClass and SecondInnerClass are passed to a WebView as a JavaScript interface, so someMethod and someOtherMethod can be called at any time, in no particular order.
  • I currently have a problem with the existing code (without the synchronized keyword) where outerMethod is called pretty much at the exact same time (I print out a log message, and they’re timestamped to the 1000th of a second) by different objects. My app then ‘does stuff’ twice because outerMethodHasBeenCalled is still false when outerMethod was called. This is not okay, and it is exactly what I’m trying to prevent. My app should only ‘do stuff’ once and only once: the first time outerMethod is called.
  • It might sound like I have multiple instances of OuterClass, but rest assured that it’s only one instance of OuterClass.

It’s important that my app ‘does stuff’ only the first time outerMethod gets called (I hope that’s evident by now). All subsequent calls are essentially ignored. Whichever inner class calls outerMethod first — doesn’t matter.

So, is it appropriate to use the synchronized keyword in this case?

  • 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-30T15:40:56+00:00Added an answer on May 30, 2026 at 3:40 pm

    Yup, given what you’ve laid out above, I’d go with:

    private synchronized void outerMethod() {
    ...
    }
    

    Note, this will have the side-effect of blocking one of the callers until the outerMethod() completes. If that is acceptable, cool. If the intent is merely that the code in outerMethod() is run once, and it is OK for the second caller not to be delayed if the first caller is running outerMethod(), you might consider:

    public OuterClass {
        private AtomicBoolean outerMethodHasBeenCalled = new AtomicBoolean();
    
        private void outerMethod() {
            if (outerMethodHasBeenCalled.compareAndSet(false, true)) {
                // do stuff
            }
        }
    ...
    

    See the JavaDoc for AtomicBoolean to grok what is going on there (assuming it is available in Android’s Java).

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

Sidebar

Related Questions

I have asked this question earlier today but I didn't provide enough information and
I hope this is ok asking here.. I Have been making an asp.net application
I hope this is allowed but I have a number of questions regarding Facebook
I hope this is a quick question I hope. I need to write some
Very inexperienced with Flash, but I hope this is simple enough for someone to
I hope I can make this question clear enough. I'm looking to put a
I need a script to edit files. Im going a bit crazy about this
I hope this is just a learning curve I'm going through. I create an
Alright, I'm going to try to explain this as best as possible: I have
I made this small location class so that I can better handle address information

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.