0
import java.util.Scanner;  

public class Initials {  
    Scanner getin = new Scanner (System.in);  
    public static void main(String[] args) {  
        String Name;  
        System.out.println("Enter your name's Initials::");  
        Name=getin.nexlinet();  
    }  
}

error : non-static variable cannot be referenced from a static context?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Hunter
  • 109
  • 1
  • 5

7 Answers7

3

First of all, Scanner objects doesn't hava a method called nexlinet, I guess you want nextLine() instead.

About the error, you can't reference a non-static variable from a static method (in this case, that method is main).

Why? Because static variables can be used even if no instances of the class have been created.

How to solve it?

  • You can declare the variable getin as static:

    static Scanner getin = new Scanner(System.in);
    
  • Or you can create an instance of the class and access to the instance field getin:

    Initials some_name = new Initials();
    // ...
    name = t.getin.nextLine();
    

Notes:

  • Try to follow Java naming conventions. Use 'mixedCase' for methods/variables and use 'CamelCase' for classes/interfaces.
  • I'd recommend you to read about access modifiers. Why? Look at the second solving way. The class Initials should provide a getter/setter method for the instance field getin, so you don't have full access on it. Also, it's a good practice to declare instance fields as private (and use getters/setters).
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Thnx, I now understand what my mistake was. Either I could declare the variable static or make the "main" as non-static. – Hunter Apr 12 '14 at 06:37
  • @Hunter the [`main()` method is a *very special* method, it should always be `public static void main(String[] args)`.](http://stackoverflow.com/questions/146576/why-is-the-java-main-method-static) – Christian Tapia Apr 12 '14 at 06:39
1

try this:

// ur code
    public static void main(String[] args) { 
        Scanner getin = new Scanner (System.in); // <-- move scanner declaration inside main
        Name=getin.nextLine();  // <-- note this change also
// ur code
0

you have to create scanner class object inside main method.

because outside declared non static variable cant accessed into static methods.

public static void main(String[] args) 
{  
    Scanner getin = new Scanner (System.in);  
    String Name;  
    System.out.println("Enter your name's Initials::");  
    Name=getin.nextLine();  
}  
Ashish Ramani
  • 1,214
  • 1
  • 17
  • 30
0

you are not aware of static and non-static variables or methods.

  1. static variable or method can be accessed any where whether it is static or non-static method.
  2. non-static method or variable cannot be accessed in static methods.

so modified code should be :

import java.util.Scanner;  

public class Initials {  

    public static void main(String[] args) {  

        Scanner getin = new Scanner (System.in);    
        System.out.println("Enter your name's Initials: ");  
        String Name = getin.nextLine();  
    }  
}

otherwise modify Scanner tostatic Scanner getin = new Scanner (System.in);

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
0

Static methods: Cannot access non static variables. Cannot invoke non static methods. Cannot use "this" keyword. Cannot use "super" keyword. Cannot override static methods. so you can't access non static objects inside of the main method because its a static method.

Bala.Raj
  • 1,011
  • 9
  • 18
0
import java.util.Scanner;  

public class Initials 
{  
    public static void main(String[] args) 
   {  
        Scanner getin = new Scanner (System.in);  

        String Name;  
        System.out.println("Enter your name's Initials::");  
        Name=getin.nexlinet();  
    }  
}
Ali Raza
  • 613
  • 5
  • 17
  • Please explain your code to be more educative. – László Papp Apr 12 '14 at 09:52
  • Sure why not,I have created the scanner object inside the main static method. If we create this outside the main method then it will not be assessable in the main method because a non static variable can not access in the static method and finally it will produce an error. After creating the Scanner object I am taking the input from user by using scanner's object and finally I am saving it in another variable – Ali Raza Apr 12 '14 at 09:59
0

import java.util.Scanner;

public class Initials {

public static void main(String[] args) { 
     Scanner getin = new Scanner (System.in);  
    String Name;  
    System.out.println("Enter your name's Initials::");  
    Name=getin.next();  
}  

}

skadam
  • 21
  • 3