user3217656
user3217656

Reputation: 3

How do I implement "this" into my Java program?

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; 
    }
}

Upvotes: 0

Views: 74

Answers (3)

GeZo
GeZo

Reputation: 96

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;
    }
}

Upvotes: 0

Sventimir
Sventimir

Reputation: 2056

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.

Upvotes: 0

Alex Yuly
Alex Yuly

Reputation: 180

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.

Upvotes: 1

Related Questions