0

It's a requirement of my school assignment that I use "this" in the following program. However, I can't quite figure out where I could put this. I keep getting a "non-static variable this cannot be referenced from a static context" error.

import java.util.Scanner;
public class PrimeNumber
{
    public static void main(String args[]) 
    {
      System.out.println("Enter the upper limit for the prime numbers computation: ");
      int upperLimit = new Scanner(System.in).nextInt();
      int count = 0;
      for(int number = 2; number<=upperLimit; number++)
      {
          if(isPrime(number))
          {
              System.out.println(number);
              count++;
          }
      }
      System.out.println("Number of primes generated: " + count);
    }
    public static boolean isPrime(int number)
    {
        for(int i=2; i<number; i++)
        {
           if(number%i == 0)
           {
               return false; 
           }
        }
        return true; 
    }
}
  • 8
    Well, `this` always references an object. Unfortunately, you're not using any objects in your code, other than a small few whose classes are part of the JDK. I don't see how you can possibly meet the requirements of this assignment, and I wonder what your teacher was actually thinking. – Dawood ibn Kareem May 07 '14 at 06:28

3 Answers3

1

The Java keyword this refers to the instance of your class that invoked an instance method. A static method is general to its class, and so you cannot reference any instance (non-static) variables from within it. You can only access instance variables like this from within an instance method, that is, a method that is not defined as static.

So, you would need to create an instance method (of which there are none in your class), in order to use this.

Alex Yuly
  • 180
  • 1
  • 9
0

This is nothing more than a reference to the object on which the method was called. Static methods on the other hand can operate without any instance of the class even exisiting, therefore they can't have reference to any object. That's why you can't use this in static method. If you really need this, you have to remove static keywords from your functions and use instance variables in those functions anyhow.

Sventimir
  • 1,996
  • 3
  • 14
  • 25
0
public class PrimeNumber
{
    public int count = 0;
    public int upperLimit;
    public static void main(String args[])
    {
        PrimeNumber pn = new PrimeNumber();
        System.out.println("Enter the upper limit for the prime numbers computation: ");
        pn.upperLimit = new Scanner(System.in).nextInt();
        pn.doCheck();
        System.out.println("Number of primes generated: " + pn.count);
    }

    public void doCheck() {
        for (int number = 2; number <= this.upperLimit; number++)
        {
            if (this.isPrime(number))
            {
                System.out.println(number);
                count++;
            }
        }
    }
    public boolean isPrime(int number)
    {
        for (int i = 2; i < number; i++)
        {
            if (number % i == 0)
            {
                return false;
            }
        }
        return true;
    }
}
GeZo
  • 86
  • 2
  • 4