-2
import java.util.Scanner;

class Program
{
    public static void main(String[] args)
    {
        Customer customer = new Customer();
        Customer.method();
    }
}

class Customer
{
    **Scanner scan = new Scanner(System.in);**

    public static void method()
    {             
        System.out.print("Name : "); // error  **non-static variable scan cannot be referenced from a static context**
        String name = scan.nextLine();
    }
}

Question :- while creating an Scanner class object outside the method() there is an compilation error :- non-static variable scan cannot be referenced from a static context but when i define it inside the method() it is working without error Why ?

Question :- How to create Scanner class Object that every class can use that single object which is defined at one place in a program. Is this possible ?

  • For starters, you should be calling customer.method() instead of Customer.method() – Tim Jul 21 '16 at 06:45
  • @Tim, that's wrong. method() is static. – Dave Jul 21 '16 at 06:46
  • It is misspelled sorry.. but my question is why the error is coming while creating scanner class object outside the method() that non-static variable scan cannot be referenced from a static context. – Dj_learner Jul 21 '16 at 06:49
  • Just remove static keyword from method name in customer class, As you have declare method as static therefore it will not allow you to access non - static variables, either remove static keyword or declare Scanner variable as static – Vickyexpert Jul 21 '16 at 06:50
  • Yes, After chaging method type static to non-static it is working. what is the reason behind if I use static method it is stating error. – Dj_learner Jul 21 '16 at 06:52

3 Answers3

1

If you want to access the Scanner from a static method, Scanner itself has to be defined static. If you want everybody to have access to the Scanner on the Customer class, define it as public static.

You may want to read up on OOP principles and on the static keyword in Java.

Dave
  • 1,784
  • 2
  • 23
  • 35
0

For your first question, you are trying to access a non static variable from a static method. Static methods can be called without having an instance of the class. However, non static variable are created per instance of the class.

For your second question, you can either make your scanner static, and if you want it to be used from multiple classes, then make it public.

class Customer
{
    static Scanner scan = new Scanner(System.in);

    public static void method()
    {             
        System.out.print("Name : "); 
        String name = scan.nextLine();
    }
}
yamenk
  • 46,736
  • 10
  • 93
  • 87
0

Please check below possible 2 solutions

Solution 1

   Scanner scan = new Scanner(System.in);

   public void method()
   {             
       System.out.print("Name : "); 
       String name = scan.nextLine();
   }

Solution 2

   static Scanner scan = new Scanner(System.in);

   public static void method()
   {             
       System.out.print("Name : "); 
       String name = scan.nextLine();
   }
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34