-1

"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();
    } 
}
8
  • nope. Make an instance or make it static. Commented Apr 6, 2017 at 10:43
  • 2
    The body of your "question" is unrelated to the title. What are you asking? Commented Apr 6, 2017 at 10:44
  • plz just write the code of above statement I'll be thankfull Commented Apr 6, 2017 at 10:50
  • @RanaHamzaKhursheed we are not here to do your homework. Show us what you did, and we might help you! Commented Apr 6, 2017 at 10:52
  • No. What have you done? What are you stuck on? We're not here to do your assignments. Commented Apr 6, 2017 at 10:54

2 Answers 2

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

    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.