• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

dowbt about forward referencing in SCJP1.5

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source : http://technoheads.blogspot.com/



Choose :
1. Compiler error complaining about access restriction of private variables of Hello.
2. Compiler error complaining about forward referencing. .
3. No Compilation error - The output is 0;
4. No Compilation error - The output is 10;

Answer 2

When I ran the above program using eclipse ,the output I see is 0.
Please correct me where I went wrong
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me also it is printing 0. But why? why not 10? And what is this Forward Referencing?

Thanks,
Geeta
 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output is 0 on the textpad.

Thanks, Naveen
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get 0 as well using NetBeans IDE.

I think the variable i gets initialized to 0 when the object is created without never running giveJ().
That declaration (int i = giveJ()) would have run fine if it would have been a local variable instead of an instance variable.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody

my explanation is like follows.but i am not sure ,am i correct

when we create a new Hello object ,it is trying to assigniing values for it's instance variables .so it starts from ivariable and going to method
.it retuns j value but private int j=100; is not still executed.so it's return default value which is assigned at class loading time.

here private keyword not affect any thing

if i am wrong plese give a correct explanation
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asanka is right. Let me explain it to you in detail. When an object is created, all the instance fields are assigned the default values according to their data types (integer:0, floats:0.0, boolean:false, references:null). Then the instance initializer expressions and blocks are executed in order of their declaration and then constructor statements are executed.

Actually the constructor is invoked first which in-turn calls super class constructor or other constructor. Then the initializer expressions and blocks are executed. This is what actually happens to your code after compilation



This is what the code will look like after compilation

 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ankit,

Thanks for the detailed explaination!!!

regds,
Jyothsna
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic