Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implicit constructor

 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys please look down the following program i coded..
after executing this program i got the result as
output
---------
0
0
0
0
...........
my question is i didn't defined any constructor in this program. even though the variables g and h are not static variables . then how can i get those output as 0 .
is this the work done by an implicit constructor or something like that .. please explain it to me .. thanks in advance ..


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For member variables there's a rule: if you don't initialize the member variable explicitly, then it will be automatically initialized with a default value (which is 0 for int, null for non-primitive variables, etc.).
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vignesh gopalakrishnan wrote:
is this the work done by an implicit constructor

Yup. only for final instance/class variable you required to set explicitly.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler generally puts a default value for all variables. However, it is bad practice to rely on this behavior. Changing compilers might change the behavior.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html



It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:The compiler generally puts a default value for all variables. However, it is bad practice to rely on this behavior. Changing compilers might change the behavior.

It should not change the behaviour if you use a different Java compiler. The Java Language Specification specifies exactly what the default values of variables of different types is. A compiler is not free to choose other default values; if it does that, the compiler would not comply to the JLS.
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:For member variables there's a rule: if you don't initialize the member variable explicitly, then it will be automatically initialized with a default value (which is 0 for int, null for non-primitive variables, etc.).



thanks for your reply jesper de jong . i had a little doubt with this . but you said that ""null for non-primitive variables"" . this statement gives me a clear definition . its damn clear for me now. thanks a ton jesper de jong .
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
vignesh gopalakrishnan wrote:
is this the work done by an implicit constructor

Yup. only for final instance/class variable you required to set explicitly.



thanks seetharaman . sorry , dont mistake me . are you sure that this is the work done by implicit constructor . because a user name jesper replied that it will automatically set to zero when it is declared ..?? please show me the difference . i think jesper meant that , there is no use of implicit constructor .
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vignesh gopalakrishnan wrote: . . . is this the work done by an implicit constructor or something like that . . . .
No.

What you want to do is to compile the code, and then inspect the bytecode with javap -c implicitconstructor. You will see the constructor, which is called <init> or similar. That calls the superclass constructor, and doesn’t do anything else. You may find the fields initialised to 0.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, it is called a default constructor.
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
vignesh gopalakrishnan wrote: . . . is this the work done by an implicit constructor or something like that . . . .
No.

What you want to do is to compile the code, and then inspect the bytecode with javap -c implicitconstructor. You will see the constructor, which is called <init> or similar. That calls the superclass constructor, and doesn’t do anything else. You may find the fields initialised to 0.


campbell ritchie .. thank for your reply . and yeah i checked it out . but i can't understand those assembly language . i can see the the phrase you said <init> . but where can i find the automatic initialization part . there is some thing like 0:aload_0 and 1:invoke special #1 . what does this mean . .. please explain it to me.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add an empty constructor, there is no change to any of the bytecode. If you add this constructor:. . . and repeat the compilation and javap call, this is what comes outCan you see the difference? There are two iconst calls, which load a constant (0) onto the stack, each followed by an instruction to put that value into a field.
You notice the first (and second) versions do not explicitly initialise those two fields at all. That must have been done by the JVM when you instantiated the class.
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you add an empty constructor, there is no change to any of the bytecode. If you add this constructor:. . . and repeat the compilation and javap call, this is what comes outCan you see the difference? There are two iconst calls, which load a constant (0) onto the stack, each followed by an instruction to put that value into a field.
You notice the first (and second) versions do not explicitly initialise those two fields at all. That must have been done by the JVM when you instantiated the class.




yeah . absolutely . and thank you very much ritchie . now i got it . a crystal cleared definition from you . thanks a ton .
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic