• Post Reply Bookmark Topic Watch Topic
  • New Topic

public static void, public void and public 'returnType'  RSS feed

 
ashlan cooper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone please explain when can I use those above. I get confused.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method is returning something, then the return type needs to be specified. Otherwise, "void" needs to be specified to indicate that nothing is returned.

If the method should be associated with the class (instead of a particular instance of the class), then the keyword "static" is used. Without "static," the method is an "instance" method. See Java Tutorial.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void = a static method which allows public access and returns nothing. This means it is available for anyone to see/use because it is public, it is associated with the class not an instance because it is static, and void always simply means there is no return value for the method.

public void = a non-static method which allows public access and again returns nothing. This means it is available to anyone just as above, but this time because it is not static it is associated with an instance not the class itself. Again because of the void there is no return type.

public 'returnType' = a public method which is showing you that where "returnType" is you may place whatever it is your would like your method to return. If you want it to return a string a method would look like


or if you wanted to return an Integer


Generally it is a good practice to search for the information on your own first prior to posting as each of these come up with tons of information if Googled. Also check out the link in marc's post. Welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!