aspose file tools*
The moose likes Beginning Java and the fly likes Does whether you can use public aMethod() or not depend on the program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does whether you can use public aMethod() or not depend on the program?" Watch "Does whether you can use public aMethod() or not depend on the program?" New topic
Author

Does whether you can use public aMethod() or not depend on the program?

Jane Hall
Greenhorn

Joined: Jul 31, 2012
Posts: 12
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?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

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.
Jane Hall
Greenhorn

Joined: Jul 31, 2012
Posts: 12
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does whether you can use public aMethod() or not depend on the program?