• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about using 'new'

 
sam thomson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im kind of confused on something,if you do say,

apple A = new apple();

does apple() get called or is it just telling A that it is a new apple() and you then have to call apple() yourself,i've seen code were it looks like both happen,im kind of confused by that?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor "apple()" gets called.

Tell you what: Head over and read this and this and I betcha you'll understand a whole lot better.
 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
jim gotti
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


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 ]
 
sam thomson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright,thanks to everyone,coming from basic style languages that just looked foreign to me
 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

No! This implies that a class is a collection of objects. That's not a correct understanding.

Think of a class as an object data type definition. An instance of that class is an object with the properties and behaviors defined in that class.

If that's not abstract enough...

Think of a class as a cookie cutter. The instances of the class (the actual objects) are the cookies created following the structure defined by the cookie cutter.
 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
...
No! This implies that a class is a collection of objects. That's not a correct understanding.
...
If that's not abstract enough...
...

I guess I am voted off the island?

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! I'd like to keep around anybody that can learn and is interested to do so.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic