Wanted to declare a local variable inside a static method. main().

Here is what my code looks like -


Code:

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?

Any Sugession would be helpful
It may technically do what you want, but that's bad style. Static values should usually be avoided because they're really just global variables, which are very easy to accidentally misuse and generally make code hard to reason about.
ok, Understood!

Thanks for the reply.

I need your view on this too, I am Declaring a Static Variable in Java

Example:

Code:

public class Scaler {
//Creating two static members
static boolean check;
static int number;
    public static void main(String[] args) {
System.out.println(Scaler.check); //Default value is false
System.out.println(Scaler.number); //Default value is 0.
 
    }
}



Output:

Code:

false
0


Is this the correct way?
It's valid code. Whether it's reasonable for what you want in any given situation is impossible to say given such a contrived example.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement