Click to See Complete Forum and Search --> : newbie java question
JoeyJoeJo
09-14-2004, 11:34 PM
I'm just getting going with java in a cs class. I have come across something I don't understand. This isn't a question on a test or anything. We are learning all about methods and variables and whatnot. One program we did is supposed to calculate the volume of a sphere. When the main method calls the method that calculates the volume, it looks like this.
Sphere ball = new Sphere(radius);
What I don't understand is, what is the "new"? What is it's purpose? Sorry if this is too vauge.
X_console
09-14-2004, 11:40 PM
"new" creates an instance of the class Sphere. So in your class Sphere you will basically have a constructor that takes a parameter (radius) and will assign that parameter to the private instance variable radius in your class. When the constructor is complete, execution returns to main() and the variable ball of type Sphere will have the radius that you provided when you called the constructor.
Hope that was clear.
MunterMan
09-15-2004, 06:59 AM
Java is an object oriented language and not a procedural language like COBOL or C or BASIC.
You do NOT have functions that are referenced from other functions.
You have objects; and it doesn't help to think of objects as functions because they are, in fact, very different.
Your line of code is creating a new object called ball which is an instance of the class Sphere... (Carry on to X_console's answer)
JoeyJoeJo
09-15-2004, 12:09 PM
I see. It's all making sense now. Thanks guys.