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? 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);}}

When the object is created, its fields are created. You have a class member:

  Private int channel = Set channel (7);   

When you do:

  New Television (20);   

The field is initialized and the setchannel is assigned to the constructor Calling is done before calling and 7 is printed from there.

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.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -