The Prime Directive - step 8

Hi there,

I have a question regarding step 8 of Prime Directive project. I don’t understand why it’s necessary to call a method like this:
System.out.println(pd.isPrime(2));

and this doesn’t work?
System.out.println(isPrime(2));

Bellow all my code. I am sure that I am missing fundamental concept, but why are we creating new object before calling method? Can someone explain that to me or show me where I can read about that?

// Import statement:
import java.util.ArrayList;
class PrimeDirective {
  public boolean isPrime(int number){
    if(number == 2){
      return true;
    } else if(number < 2){
      return false;
    }
    for(int i = 2; i < number; i++){
      if(number % i == 0){
        return false;
      }
    }
    return true;
  }
  // Add your methods here:
  
  
  public static void main(String[] args) {

    PrimeDirective pd = new PrimeDirective();
    int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
    System.out.println(pd.isPrime(2));
  }  

}

Hi @chip0667552693 welcome to the forums :slight_smile:

Java is object-oriented out of the box so… when you are in your main method, you must instantiate the class objects that have the methods you want to use.

The exception to this is static context. That is, methods that shouldn’t depend on class objects. You are right that isPrime() makes more sense in this category.

For reference: Java Static - Stack Overflow

Thanks! If i understood it correctly: everything in java need an object, except static context and all I need is to live with that :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.