This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "default constructor" Watch "default constructor" New topic
Author

default constructor

Rama Raju
Greenhorn

Joined: Jul 07, 2000
Posts: 12
public class AQuestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
In the above example, it is printing 0. Can u explain how zero is getting printed?
Javix Protocol
Ranch Hand

Joined: Mar 21, 2000
Posts: 57
Study the basics clearly and then put the question.
Try Simon Roberts "The Complete Java2 Certification Study Guide"
buddy.


Write Once , Run Anytime ,Anywhere Forever And Smile Happily.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try making int j = 10 as static int j= 10. This should print 10 instead of 0.
I think this also explains why it's printing 0.
Prabhu.
Rama Raju
Greenhorn

Joined: Jul 07, 2000
Posts: 12
Javax Protocal,
Thanx for your suggestion.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Just by re-arranging the order of declaring int i and int j will give you the result 10. Try the following code:
<pre>
public class AQuestion
{
private int j = 10;
private int i = giveMeJ();
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
</pre>
Reason:
12.4.1 of the JLS says A class or interface type T will be initialized at its first use, which occurs if ...
"A non-constant field declared in T (rather than inherited from a superclass or superinterface) is used or assigned."
When an instance of the class is created, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values. It means i = 0 and j = 0 when the object is first created.
(In your version)
The assignment statement i = giveMeJ() then starts the initialization process. At that stage, j has not been initialized. It means j is zero, therefore giveMeJ() returns zero to i. So i is assigned a zero again. Next, j is initialized to 10. That's why it prints a 0, not 10.
(In my version)
The assignment statement j = 10 is the first assignment statement, so j is assigned 10. Then the statement i = giveMeJ() assigns the current value of j (10) to i. So the program prints 10.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default constructor
 
Similar Threads
Variable initialization
forward refrerences
Forward Referencing
please explain why the compiler is not complaining and why the output is 0
why get's printed