J2EEOnline J2EE  






Java TM Fundamentals I  «Prev 
Adding behavior with methods

Void Data Type

I want to clarify the usage of the void data type. The void data type is interesting in that it does not actually represent any data at all. It is a valid data type, but it represents the absence of data. In other words, in the eat() method a return type of void indicates that eat() does not return any data.
public void eat(){
  System.out.println("Within the eat method");
}
The purpose of a method is to complete a specific task within a program.
The method can return a value once the task has been completed.
In some cases, a method must produce a result. If it does not, then it is considered void.
The type of value that a method can return is written to the immediate left side of the method name.
If the method does not return a data type, then type void to the immediate left of the method name.
The task that a method carries out is included between an opening curly bracket "{" and a closing curly bracket "}".
Here is an example:
public class House {
   long propertyNumber;
   String propertyType;
   byte Stories;
   public int Bedrooms;
   double MarketValue;
   void displayHouse() {
     System.out.println("Within the displayHouse method");
   }
}