Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need a little help in this class

 
Anand B Raju
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anand B Raju
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody respond dear
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
natarajan raman
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah!!!Chandra I too agree with you.

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic