Static variable in static method - how to use?

In summary, your code is correct, but you may want to consider using a local variable instead of a static one.
  • #1
bikashdaga
4
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
  • #2
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 bikashdaga
  • #3
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.
 
  • #4
I am really sorry for the mistake of tagging, thanks for pointing it out. Will make sure to tag properly.
 
  • Like
Likes jedishrfu

FAQ: Static variable in static method - how to use?

What is a static variable?

A static variable is a variable that is associated with a class rather than an instance of that class. This means that the variable can be accessed and modified by any instance of the class or any static method within the class.

What is a static method?

A static method is a method that is associated with a class rather than an instance of that class. This means that the method can be called without creating an instance of the class, by using the class name followed by the method name.

How do I declare a static variable in a static method?

To declare a static variable in a static method, you need to use the static keyword before the variable name. For example: static int myVariable = 5;

Can a static method access non-static variables?

No, a static method cannot directly access non-static variables. This is because non-static variables are associated with instances of the class, while static methods are associated with the class itself. However, a static method can access non-static variables by creating an instance of the class and then accessing the variable through that instance.

Why use static variables in a static method?

Static variables in a static method can be useful for storing data that needs to be shared among all instances of the class. They can also be used to keep track of the number of instances of a class or to store constants that are used by multiple methods within the class.

Similar threads

Back
Top