-2

"write a program in java that declare a class with one integer data member and two member functions in() and out() to input and output data in data member."

My current code is as follows.

import java.util.Scanner; 
public class Operator 
{ 
    static int a;
    public static void input() { 
        Scanner in=new Scanner(System.in); 
        System.out.println("Enter the number:"); 
        a=in.Nextint(); //Here is problem 
    }

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

    public static void main(String[] args) 
    {
        input();
        output();
    } 
}
Colin
  • 3,394
  • 1
  • 21
  • 29
Hamza Khursheed
  • 2,399
  • 2
  • 15
  • 17
  • nope. Make an instance or make it static. – matt Apr 06 '17 at 10:43
  • 2
    The body of your "question" is unrelated to the title. What are you asking? – Colin Apr 06 '17 at 10:44
  • plz just write the code of above statement I'll be thankfull – Hamza Khursheed Apr 06 '17 at 10:50
  • @RanaHamzaKhursheed we are not here to do your homework. Show us what you did, and we might help you! – Thomas Böhm Apr 06 '17 at 10:52
  • No. What have you done? What are you stuck on? We're not here to do your assignments. – Colin Apr 06 '17 at 10:54
  • import java.util.Scanner; public class Operator { static int a; public static void input() { Scanner in=new Scanner(System.in); System.out.println("Enter the number:"); a=in.Nextint(); //Here is problem } public static void output() { System.out.println("Number is:" + a); } public static void main(String[] args) { input(); output(); } } – Hamza Khursheed Apr 06 '17 at 11:01
  • I have declared a static variable and want to input value in that in a member function using Scanner but ... – Hamza Khursheed Apr 06 '17 at 11:07
  • @RanaHamzaKhursheed Please modify your question: add what is the problem with your current code (what doesn't work as expected). – Thomas Böhm Apr 06 '17 at 11:12

2 Answers2

1

You seemed to be confused w.r.t instance variables and local variables.

You can always declare a "local variable" inside a static method. main() for example is a static function and we always declare variables inside it.

So your creation of a variable "in" of type Scanner inside input() function is perfectly fine.

However, you "cannot" access instance variables and instance methods from static methods.

This post on stack overflow gives a full and complete answer: Can non-static methods modify static variables

As far as your code is concerned, there's a minor error in the code. The function call to read an integer is "nextInt" and not "Nextint". Java generally uses camel-case to define all its methods. So be careful with the method usage.

The modified code should be this:

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();

    }
}
Community
  • 1
  • 1
paratrooper
  • 434
  • 1
  • 10
  • 18
0

short answer - NO reason is simple too, that is - it will violates the definition of static i.e. accessible in other class without creating a object(also called instance) of the class.

enter image description here

But, what if we try to do static variable in a non-static method ? In that case, YES we can do that. Because we have to create a instance (object) of the class to use that method. So, that doesn't violates the definition.

enter image description here