Edit: dabig2 has a much better and technically correct explanation than I do. You should probably just read his, lol. I think I understand things a bit better after reading it too.
Don't mean to hijack your question, MutantCyborg, but I'm starting to learn Java myself and thought I'd give it a try too. Also, is it just me or does Slavik seem to know everything?
Does it matter that I made the primy object static so it could be referenced from both main and goTime? Is there a better way to do that?
Any constructive criticism would be much appreciated!
(For example, I didn't know before that scanners had to be closed for gc)
I'm relatively new to this as well and I don't really know how everything works yet, but to me, there's no reason for you to have a static variable defined in the class.
Basically, what your code does is use the
static main method to initialize an instance of Primes. At that point, you can manipulate the Primes object however you want. Since the
goTime and
isPrime methods belong to the Primes object, you don't need to do anything special to reference primy like creating a static variable (If you need a method to reference the object it belongs to, the
this keyword works).
Also, if you're trying to use other methods in the same object, you don't need to indicate the object they belong to. For example,
primy.isPrime(i) can be shortened to
isPrime(i). In that case, what's happening is that, inside your
main method, the object
primy that you initialized calls the member method
goTime. You can think of
goTime as running inside the object
primy. Since
isPrime is also in the same object, you can reference it without needing to specify the object it belongs to.
I'm not sure my explanation is correct since, as I said, I'm not exactly sure how everything works yet, but that's my understanding of it.