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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:14:02+00:00 2026-06-10T18:14:02+00:00

In C/C++ we use static local variables for maintaining a method’s state. But why

  • 0

In C/C++ we use static local variables for maintaining a method’s state. But why it is not supported in Java?

Yes, I can use an static field for this purpose. But isn’t it a bit weird to create a field for maintaining only one method’s state?

  • 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-10T18:14:04+00:00Added an answer on June 10, 2026 at 6:14 pm

    You have found the only solution.

    Java dropped a number of complexities from C++, and this was one of them.

    Static variables scoped to a function do nasty things to you in concurrency (e.g. strtok is a famously nasty one to use with pthreads, for exactly this reason).

    In general, what you want is an object with state. The function in question should then have an object-level variable. Then you can create instances that each maintain state.

    Much easier to understand/maintain/etc.

    If you truly need to maintain state as a singleton, then static fields are it.

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

Sidebar

Related Questions

Why cant i use static variables in events handling methods like this private void
I know that you can use static libraries to manage dependencies for iOS apps.
I need to explain myself why I do not use static methods/propertis. For example,
Is it reasonable to use private static variables to establish invariants in your class?
Do Java ThreadLocal variables produce thread-local values if they are used as instance variables
I can't get the syntax correct for with static variables and c++ classes. This
Possible Duplicate: When to Use Static Classes in C# Questions in the title..........i would
How often should I use static methods generally? If I have like: Class1 _class34
I Want to know which one is preferred while coding to use Static Methods
if I define a macro, or use static const value, in an embedded system,

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.