• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance Variabe

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls can someone explain these codes.

class DirtyCode {
int j = i; // won't compile
int i = 20;
int initI() {
return i;
}
}

2. class DirtyCode {
int j = getI(); // compile and prints j = 0;
int i = 20;
int getI(){
return i;
}

Why is j = 0 and not 20;
Why did it compile

 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instance variables exist on the heap with default values, 0, null, or false, before the initializers or constructors are executed. The second code fragment obtained the value of i before the initializer was reached, so it got the default value.

variables in initializers are subject to a "declare before read" rule. A variable cannot appear on the right side of an = in an initializer until that variable is declared.

method calls are not subject to that rule, a method can be called before it is declared. If the method returns a value, the value can be used. The return statement in getI() appears after i is declared. Also, the expression in a return statement is not an initializer. For these reasons, "return i;" is legal.
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Thanks for the 5 star answer, but am still not clear why the value
of j was set to 0, instead of the returned value.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike has very well explained ur doubt:


instance variables exist on the heap with default values, 0, null, or false, before the initializers or constructors are executed. The second code fragment obtained the value of i before the initializer was reached, so it got the default value.



All the instance variables are already initialized to values 0 before the execution of initializers or constructors.

In ur second code, the expression
int j = getI();
is reached before i is initialized to 20. Since i is initialized to 0 before, it gets the value of i as 0. So u get the result as 0.

Just change ur code to:

Now u can see the difference
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike & Animesh,
Thanks a million of clearing ma doubt.
Have a swell weekend.
se anjuma( means bye for now -hausa language)
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Animesh,

int x=y; not possible because not declared yet
int y=10;

When you create any instance variable it'll automatically initializes but if you don't create first on the heap then it won't compile.So before initilize any variables value with the variable name pls look at the variable declaration first whether it's been declared or not clearly.

I think thats enough for you. If you have any query pls write me at
callanwar@gmail.com

Regards
Anwar Hossain
SCJP2,MCP
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a follow up quetion regarding the

variables in initializers are subject to a "declare before read" rule.



In code # 2, int j = getI(), whick prints 0. It printed the value 0 because it returned the initialized value of "i" which was 0 right? But following the "declare before read" rule, does it follow that there should have been a compiler error because during the initialization of j, the variable i was not yet declared and yet the method getI() used the initialized value of i.
 
reply
    Bookmark Topic Watch Topic
  • New Topic