This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes implicit constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "implicit constructor" Watch "implicit constructor" New topic
Author

implicit constructor

vignesh gopalakrishnan
Greenhorn

Joined: Jun 27, 2012
Posts: 20
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Jun 27, 2012
Posts: 20
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

Joined: Jun 27, 2012
Posts: 20
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

Joined: Oct 13, 2005
Posts: 38044
    
  22
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

Joined: Oct 13, 2005
Posts: 38044
    
  22
By the way, it is called a default constructor.
vignesh gopalakrishnan
Greenhorn

Joined: Jun 27, 2012
Posts: 20
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

Joined: Oct 13, 2005
Posts: 38044
    
  22
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

Joined: Jun 27, 2012
Posts: 20
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

Joined: Oct 13, 2005
Posts: 38044
    
  22
You’re welcome
 
 
subject: implicit constructor
 
Similar Threads
order when constructing
instance member access.. help me
Overriding and redefining
inheritance
Automatic Casting