A questions from a java beginner

Project Link https://www.codecademy.com/courses/learn-java/projects/java-prime-directive
My code:

I am new to Java Programming and I want to ask a question I had when I was doing the project.

In line 33 I tried to print isPrime(7) but I had an error. I then fixed it by printing pd.isPrime(7) and it worked. I am really confused about this since in line 24, I was able to call isPrime(number) directly without the prefix pd. . I would like to know when I am able to call a method directly.

Could you help me? I really appreciate it!

Hello @johnw9620667255, welcome to the forums! That is because on line 24, you call the isPrime method inside the PrimeDirective class. Since both methods are non-static, they can interact with each other. Within the main method, though, since it is not a static method, needs to be called on an instance of the PrimeDirective class. Here is an article on static methods.

Thank you so much for your help! I now have a better understanding of static and non-static methods.

1 Like