aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need a little help in this class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need a little help in this class" Watch "need a little help in this class" New topic
Author

need a little help in this class

Anand B Raju
Ranch Hand

Joined: Sep 22, 2004
Posts: 172
class New{
public int i = m();
public int j=1;
public int m(){return j;}
public static void main(String [] args){
New n = new New();
System.out.println(n.i);

}}

when i execute this class i get the output of 0(Zero).
can anyone of u plz tell why this is happening so.


SCJP 1.4<br /><a href="http://www.geocities.com/sunjava4u" target="_blank" rel="nofollow">SUN JAVA 4 U</a>- links and resources for SCJP aspirants
Anand B Raju
Ranch Hand

Joined: Sep 22, 2004
Posts: 172
Somebody respond dear
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In Chapter 7 of Thinking in Java, Bruce Eckel describes what I believe to be a similar situation -- even calling the unexpected zero value a "mystery." He explains that, "The storage allocated for the object is initialized to binary zero before anything else happens." After this, "Member initializers are called in the order of declaration." So in certain situations, this can result in a primitive member being zero rather than the value you might expect it to be initialized to.

Eckel's advice is, "Do as little as possible to set the object into a good state [with constructors and initializers], and if you can possibly avoid it, don�t call any methods."

Ref: http://www.codeguru.com/java/tij/tij0082.shtml

Also see the Java Language Specification, Section 8.3.2.3, "Restrictions on the use of Fields during Initialization."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
[ September 23, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
Does the above explain the situation where in the positions of i and j are swapped


In this case the result is 1.
natarajan raman
Greenhorn

Joined: Apr 10, 2004
Posts: 26
Hi PNS,

I think Marc's explanation is more clear...Please see the JLS reference given.

public int j=1;

public int i = m();

The variable initializer for i uses the class method m() to access the value of the variable j and since j has been initialized by its variable initializer well before i, it prints 1.

In the first case ,
public int i = m();
public int j=1;

The variable initializer for i uses the class method m() to access the value of the variable j before j has been initialized by its variable initializer, at which point it still has its default value.


Hope this helps. Thanks a lot Marc!!!
[ September 30, 2004: Message edited by: natarajan raman ]

nata.r<br />**********************************<br />To win is not always success<br />And to lose is not always failure.<br />**********************************
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
Hi

What is the real "mystery" here??


when m() is called j is unitialized...so i will get the default value for int ie; 0 and output will be 0



Here before calling method m(), j is intialized to 1.So the out put will be 1.

this is the simple reason for this.....am i right?

Chandrasekhar S.

[ September 30, 2004: Message edited by: Chandra Sekhar ]
[ September 30, 2004: Message edited by: Chandra Sekhar ]

"Luck is when opportunity meets preparation"
natarajan raman
Greenhorn

Joined: Apr 10, 2004
Posts: 26
Yeah!!!Chandra I too agree with you.

Thanks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think the "mystery" centers around what "initialization" of primitive fields really means. As we know, if primitives members are not "initialized" with an explicit value, then they default to an "initialized" value of zero.

But as Eckel explains, "The storage allocated for the object is initialized to binary zero before anything else happens." After this, "Member initializers are called in the order of declaration."

We've seen in the above code that if we reference an apparently uninitialized primitive, then it will already have a value of zero (simply due to the storage having been zeroed). But if no explicit value is specified for this variable, then does it actually "get initialized" (reset) to zero, or does nothing happen? In other words, is the default "initialization" to zero actually just a side effect of this memory clearing?


[ October 01, 2004: Message edited by: marc weber ]
Anand B Raju
Ranch Hand

Joined: Sep 22, 2004
Posts: 172
hello marc,
i think that the default "initialization" for member variables is not a side effect of memory clearing . with whatever knowledge i have i think that when u create a new member variable it gets it default value irrespective of the fact whether it will be initialized later or not.if it is initialized to some other value then that value is gets stored in that variable and similarly if it is not initialized to any value explicitly then the default value persists.
hope this is helps.
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
"The storage allocated for the object is initialized to binary zero before anything else happens."

so why does
int a=b;
int b;
gives a forward reference error.
I think that 0 the default value of b should got initialized with a.
or i m missing or mixing some thing.
I feel like craming with out getting the concept rite.



Mohit Agarwal.
Would be SCJP.
"The will to win is worthless if u do not have the will to prepare"
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
int a=v();
int x=1000;
int b=v();

public int v()
{
System.out.println("x is"+x);
return x;
}

isnt this situation like when variable a is initialized the v() method is trying to access x, which is still not known to the compiler, since its declared after a.so it should actually give the same illegal forward refference error rite ??!! why it isnt giving that error in this situation. but giving error only when we are directly trying to assing x to a.


Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi Anand,

Look into the foloowing important codes :::::

public int i = m();
public int j=1;

the first line is executed first.
there the function by default returns zero value ass at that time j is not 1.

Arnab
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: need a little help in this class