java - How does the constructor work while initializing an object? -
The output of this code is 7 20.
Why does 7 print print before and after 20 that? When the object is created, its fields are created. You have a class member: When you do: The field is initialized and the The object is made up of and provided values (or the default value if no value is specified). You may think about preparing this example after the preparation and initialization of these members. Constructor is called. For further detailed information, refer to. Public Class Television {Private Internet Channel = Set Channel (7); Public Television (int channel) {this.channel = channel; System.out.print (channel + ""); } Public int set channel (int channel) {this.channel = channel; System.out.print (channel + ""); Return channel; } Public static zero main (string agr []) (new television (20);}}
Private int channel = Set channel (7);
New Television (20);
setchannel is assigned to the constructor Calling is done before calling and 7 is printed from there.
Comments
Post a Comment