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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:46:16+00:00 2026-06-13T12:46:16+00:00

In Java can a method have anything close to a static variable in C?Although

  • 0

In Java can a method have anything close to a static variable in C?Although Java doesn’t provide one
That is,it would be initialized only once and keep latest value in subsequent recursive invocations

I could pass it back to the method to have the latest value and achieve ‘initalize only once’ based on some condition which holds true only once

  int fun(.....,Nthcall,PseudoStatic)
   {if(NthCall==1)
     PseudoStatic=10
    //rest of code
      Pseudostatic=100
       fun(.....,Nthcall+1,PseudoStatic)
    }

Isn’t there something better?

  • 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-13T12:46:17+00:00Added an answer on June 13, 2026 at 12:46 pm

    We can do something like this

    public void test()
    {
        StaticVar<Integer> s1 = new StaticVar<Integer>(){};
    
        StaticVar<Long> s2 = new StaticVar<Long>(){};
    
        Integer v1 = s1.get();
        System.out.println(v1);
        s1.set( v1==null? 1 : v1+1 );
    
        Long v2 = s2.get();
        System.out.println(v2);
        s2.set( v2==null? 1 : v2*2 );
    }
    
    public abstract class StaticVar<V>
    {
        public V get()
        {
            return (V)class2value.get(this.getClass());
        }
    
        public void set(V value)
        {
            class2value.put(this.getClass(), value);
        }
    
        static WeakHashMap<Class,Object> class2value = new WeakHashMap<>();
    }
    

    It’s not thread safe though. We can simply add synchronized(class2value). Or use a weak concurrent hash map.

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

Sidebar

Related Questions

In java can an instance variable and a method have the same name without
Can you override method in java without using annotations? Because eclipse doesn't support it
I'm looking for a parser that can extract methods from a java class (static
I have a programming problem in Java: Can I define a different method for
In java can an abstract method be anything other than public? Are abstract methods
This is on Java 6 Can I have a common method to handle my
I have a working Java app that can find the most recently created file
How can hibernate can access a private field/method of a java class , for
The method show(); in class Windows for java.awt in deprecated. What can I use
How can I replace a  b by a b in Java ME? The replace() method

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.