Static variable in static method - how to use?

AI Thread Summary
The discussion centers around a Java code snippet where a user is attempting to declare a local variable inside the static method `main()`. The code defines a static integer variable `a` within the `Operator` class, which is used to store user input through the `input()` method and is then displayed by the `output()` method. A key point raised is that the static variable `a` is shared across all instances of the `Operator` class, meaning any changes to `a` will affect all instances, which could lead to issues in concurrent contexts. Additionally, there was a note about the incorrect use of code tags, indicating that the user mistakenly labeled their code as Python instead of Java. Overall, the discussion highlights concerns about the use of static variables and proper code formatting.
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?
 
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top