wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes void keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "void keyword" Watch "void keyword" New topic
Author

void keyword

christopher pam
Greenhorn

Joined: Jan 12, 2005
Posts: 5
i have another problem,here's a code i just got from a book;


public class Grade
{
private String courseName(String name);

public void setCourseName(String name)
{
courseName = name;
}

public String getCourseName()
{
return courseName;
}

public void displayMessage()
{
System.out.printf("Welcome to the grade book for\n%s,getCourseName());
}

public static void main(String args[])
{
Scanner input = new Scanner(System.in);

Grade myGrade = new Grade();

System.out.println("Initial course name was Architecture");
System.out.println();

System.out.println("Please enter the course name:");
String name = input.nextLine();
myGrade.setCourseName(name);
System.out.println();
myGrade.displayMessage();
}
}



setCourseName,considering the fact that it is void which means it shouldnt return any data when it completes task,but in this case i think it does cos the method's being called from the main method(myGrade.setCourseName) and thats when the program prompts you to enter course name after executing.after keying in the course name data is then returned to the system for the line "Welcome to the grade book for ......" to display when the displayMessage is invoked.am i right?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

"...but in this case i think it does cos the method's being called from the main method(myGrade.setCourseName)"

No, the setCourseName(...) method does not return any data. It can't because it's declared as void. The method does change the state of the Grade object that you're calling it on, but it does not return a value. Are you confusing those two concepts?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
Consider Paul's rocket mass heater.
 
subject: void keyword
 
Similar Threads
Hibernate Configuration Loading Problem
[Resolved]java.util.NoSuchElementException problem after displaying the information from text file
Netbeans and Ctrl + z (end of file)
Compilations error
JComboBox Model Help