• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors help  RSS feed

 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So while experimenting with constructors, repeating constructors with the same parameters, and with different parameters.
I got an output that I would like help with -explaining how I got it.

I made 2 classes. a "support class" (has all the info) and an "execute class" (executes info).

Support:


Execute:


the Output:

the video name is frozen
Second constructor running
Constructor running!
the video name is frozen

Output Explanation:
constructor call number 1 and 3 are the same (essentially) and both refer
to the same constructor.

My Question: if both call #1 and #3 refer to the same constructor, why is the output for #1 "the video name is frozen"

while the output for #3 used both methods in the accessed constructor-with the resulting output as
"Constructor running!"
and
"the video name is frozen"


UPDATE: I double checked the output-and this time made sure to scroll up ... its the same result
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output should be:

Constructor running!
the video name is frozen
Second constructor running
Constructor running!
the video name is frozen

Are you absolutely sure you're seeing the output you described? How are you running this program? From the commandline, or inside an IDE?

By the way I've added some formatting to your post to make it easier to read. You can read about how that works on our UseCodeTags page.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Weston wrote: . . . refer to the same constructor, why is the output for #1 "the video name is frozen" . . .
Because you told the code the name was Frozen.

Your no 1 and no 3 do not simply call the same constructor. One calls the constructor on one object and the other calls the constructor on another object. The code use in No 1 and No 3 is exactly the same, so you should get the same resultant output.
You call a constructor which says to use Frozen, so it prints Frozen. Then you call a constructor with Mulan, and it prints the output. Then you call a constructor on a third object which uses Frozen again. So you get …Frozen second constructor…Frozen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!