This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Is there a limitation on when you can use just 'public'? For example,
Sometimes using public aMethod() seems to be fine, but sometimes an error message appears saying that its an invalid method declaration and that a return type is required (I assume this means it needs to be public void, or public int etc.). It seems to depend on the program... but why?
You always need a return type in a method declaration. Anything you've seen that didn't have one (and worked) wasn't a method declaration, and must have been something else. Possibly a constructor, which doesn't have a return type and has the same name as the class.
If you can show us an example where it seems to work without, we can explain what's going on there.
Joined: Jul 31, 2012
Thanks for your reply. I haven't come across constructors yet. You are right though - the public 'methods' have the same name as the class.
I've just googled constructors. I think I might try learning Java from a different place (I was using a book) - constructors seem like a very basic and important thing to know about!