aspose file tools*
The moose likes Beginning Java and the fly likes How does this program work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does this program work?" Watch "How does this program work?" New topic
Author

How does this program work?

Sky Wallen
Greenhorn

Joined: Mar 17, 2013
Posts: 12

I'm just confused on why this program even runs. I understand that the main() section is what runs the program, but in that part, it looks like I just created an object called "noob". Does java automatically run each object that is created.

Also, I am confused on the bolded red section of the program (Line 5). What does that line do? Is that a method? Why do I even need to include that there?

Please help! Thanks in advance!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61222
    
  66

The code you pointed out is a constructor. It executes when a new instance of the class is created. So its execution is triggered in this case by instantiating the object in main().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

You create a new Salutation object in the main method, in line 17.

When you create a new object, a constructor is executed to initialize the object.

Lines 5 to 14 are the constructor of the Salutation class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sky Wallen wrote:
I'm just confused on why this program even runs. I understand that the main() section is what runs the program, but in that part, it looks like I just created an object called "noob".


You created an object. It doesn't have a name. Objects don't have names. You have a variable, which is named "noob", and you set that variable to refer to that object.

Does java automatically run each object that is created.


No. There's no such thing as "running an object." You're executing the constructor you told it to run when you created the object.

Also, I am confused on the bolded red section of the program (Line 5). What does that line do? Is that a method? Why do I even need to include that there?


That code is the declaration of a cosntructor for your Salutaion class. A constructor's job is to put a newly created object into a valid initial state. So when you do new Salutation(), the new operator creates an object, the identifier "Salutation" says that it will be of class Salutation, and the argument list in the parens tell Java which constructor to run. Since you provided empty parens, it says to run the constructor that takes no args--in this case, the one you declared on line 5, and the body of which continues until you close it on line 14.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How does this program work?