Static variable in static method - how to use?

Click For Summary

Discussion Overview

The discussion revolves around the use of static variables within static methods in Java, particularly focusing on the implications of using a static variable across multiple instances of a class. Participants explore the behavior of static variables and their impact on instance methods in a potentially concurrent context.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code example demonstrating the use of a static variable in a static method and questions whether their implementation is correct.
  • Another participant clarifies that the code is in Java, not Python, and points out the importance of using the correct code tags.
  • Concerns are raised about the shared nature of the static variable "a," which could lead to issues if multiple instances of the Operator class are used simultaneously, as changes by one instance could affect others.
  • A later reply emphasizes the potential problems that could arise in a parallel context, where simultaneous modifications to "a" could lead to unexpected behavior.
  • One participant acknowledges their mistake in tagging and expresses gratitude for the correction.

Areas of Agreement / Disagreement

Participants generally agree on the implications of using static variables in a shared context, but there is no consensus on the best practices for handling such variables in concurrent scenarios.

Contextual Notes

Limitations include the lack of discussion on specific solutions to manage static variables in concurrent environments and the absence of detailed exploration of alternative approaches to variable management in Java.

bikashdaga
Messages
4
Reaction score
2
I wanted to declare a local variable inside a static method main().

My code looks like this -

Java:
class Operator
{
    static int a;
    public static void input() {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the number:");
        a=in.nextInt(); //this is nextInt and NOT Nextint
    }

    public static void output() {
        System.out.println("Number is:" + a);
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        input();
        output();

    }
}

Is it something I am doing right?

I have read this [Spam link deleted by the Mentors] to understand if what I am writing is correct or not but I believe asking here will give me some sort of answer.
 
Last edited by a moderator:
Technology news on Phys.org
bikashdaga said:
I wanted to declare a local variable inside a static method main().

My code looks like this -

Java:
class Operator
{
    static int a;
    public static void input() {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the number:");
        a=in.nextInt(); //this is nextInt and NOT Nextint
    }

    public static void output() {
        System.out.println("Number is:" + a);
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        input();
        output();

    }
}

Is it something I am doing right?

I have read this article [link removed] to understand if what I am writing is correct or not but I believe asking here will give me some sort of answer.
First off, your code sample is in Java, not Python. In your code tag you wrote this:
[code lang="python" title = "code"]

Please take a look at the post at the top of this section that describes how to use code tags in your sample code sections.

Second, your static variable is one that is shared by all instances of your Operator class. If one instance changes a, this will affect all instances of that class. Is that the behavior you want?
 
  • Like
Likes   Reactions: bikashdaga
This one instance of "a" will be especially painful if your Operator instances are used in a parallel context ie two instances processing at the same time one changes it but before it can use the changed "a" value the other instance changes it.
 
I am really sorry for the mistake of tagging, thanks for pointing it out. Will make sure to tag properly.
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
957