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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:45:13+00:00 2026-06-02T01:45:13+00:00

In C, I can create a static variable in a function. The space for

  • 0

In C, I can create a static variable in a function. The space for that variable is not allocated along with the function variables, it’s allocated at program startup time. (Don’t mock my C too harshly, I’ve been programming in Java for way too long 🙂

void myFunc(){
  static SomeStruct someStruct;
  someStruct.state=INITIALIZED;
  goDoSomethingInteresting(MY_COMMAND,&someStruct);
}

In Java, if I want to do something similar, I create a class variable and then use it.

Class TheClass {
   SomeStruct someStruct = new SomeStruct();
   void myFunc(){
     someStruct.setState(INITIALIZED);
     goDoSomethingInteresting(MY_COMMAND,someStruct);
   }
}

My question is what is the best practice for doing something like this? I’d really like to associate my variable someStruct with my function myFunc, because myFunc is the only code that should know about or use someStruct, but there’s no way to make that association except to put the variable close to the function in code. If you put it above, then the Javadoc for the function looks wonky, if you put it below, then it’s not very clear that they belong together.

Normally I would just create someStruct locally, but in my case it’s very expensive to create someStruct, and I call myFunc in a tight loop.

  • 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-02T01:45:15+00:00Added an answer on June 2, 2026 at 1:45 am

    A small class will associate someStruct with the behavior of myFunc cleanly, but it’s extra overhead for understanding and maintaining things. Might be worthwhile, might not be.

    class TheClass {
       MyFuncBehavior myFuncer = new MyFuncBehavior();
       void myFunc() {
          myFuncer.myFunc();
       }
    }
    
    class MyFuncBehavior {
       private static SomeStruct someStruct = new SomeStruct();
    
       public void myFunc() {
         someStruct.setState(INITIALIZED);
         goDoSomethingInteresting(MY_COMMAND,someStruct);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create a contact that is not mail enabled, but how do I
One can create an anonymous object that is initialized through constructor parameters, such as
I can create function pointers in Fortran 90, with code like real, external ::
When we create a directshow filter and register it, static variables/functions are shared in
can I use max function in a variable in XSLT 1? I need to
In C, you can have a static variable within a method, which can remember
Yesterday I learned from Bill Venables how local() can help create static functions and
I have been trying to create a pointer variable in the called function and
I wanted to create a counter that updates by one, every time it satisfied
How can create a view with mutiple images, something similiar to the photo app

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.