You are actually calling the constructor of class "apple". There's nothing wrong if you haven't defined constructor when creating the "apple" class, Java takes care of that for you by assigning argumentless and bodyless constructor. Since "apple" is a class, a type of data, a cookie cutter, and "A" is an "apple" (the cookie actually cut by the cutter), it would make much more sense to call "A" that is embodiment of everything an "apple" is: all that "apple" has as a state and can do (you don't call the class, you call its objects)... Like:
You've created a new red apple called "A". Hope that helped...
it should be noted that you do not have to define a constructor for your class as G said..Java will handle it for you. However if you define even one constructor, Java will no longer automatically create that default constructor for you....you are now in charge or handling all of them
so if you want a constructor to define the color of an Apple object, then you MUST write a default constructor as well, even if there is no body to it.
so now in main if you create an apple say
it will call the default constructor, which will in turn call the other constructor with the parameters of red and 5.
if you write
it will call the constructor with the proper parameters, in the case, the other constructor and set the color to green, and number of apples to 3.
here is a quick read on constructors [ August 21, 2004: Message edited by: jim gotti ]
Joined: Aug 14, 2004
alright,thanks to everyone,coming from basic style languages that just looked foreign to me
Joined: Feb 24, 2004
I am glad I didn't have to go through that shift from procedural to object- oriented (thanks to friendly suggestions from people from this forum), hewww... Try to think of a class as a rolodex and objects derived from that class as particular pages from that rolodex filled up with names, phone numbers etc... the real fun starts when you reach the inheritance part of Java... There are number of books written on the Java and OOP subjects, one of the best is "Head First Java", don't be fooled by its unconventional appearance- it is a serous intro into Java and has about the best explanations for the key features of Java... Have fun,
I never think of this situation as a call to a constructor. I think of it as invoking an operator...the new operator. There are a few different kinds of operators in programming languages, the unary (only takes one argument, such as ++ as in a++), the binary (takes two args as in a+b), and even the ternary operator (takes three args as in a<b? a:b). Operators typically take whatever range of arguments they work over and "operate" on them (hence the name), replacing those arguments and the operator itself with the result of the operation. So, using a+b as an example, the + operator takes a and b, adds them together, and then replaces "a+b" with the result, which is the sum of a and b.
The new operator is a unary operator, but instead of taking as an argument an actual variable, it takes a method...in particular, a constructor. As far as I can see, it does three things: (1) requests a block of memory from the JVM for the object about to be constructed, (2) instructs the JVM to run the specified constructor on the newly requested block of memory, and (3) returns a reference to that block of memory into which the new object was constructed.
Typically, the new operator hands back a reference to another operator, the assignment operator (=), as in:
In the above code, the steps are: (1) new asks the JVM to give back a chunk of free memory big enough to contain a Foo object (ever wonder why Java requires that all constructors be methods of the same name as the class itself? This is so the new operator automatically can deduce what class is being constructed without requiring the programmer to provide extra information), (2) new asks the JVM to run Foo's default constructor method--Foo()--to initialize said object into that memory block, (3) new returns a reference to the new chunk of memory, (4) the assignment operator (=) uses the returned reference as one of its arguments and assigns that reference to its other argument, an object reference called "f".
That's pretty much all there is to it.
sev [ August 26, 2004: Message edited by: sever oon ]