• 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

Question Of The Day !!

 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Try this question:
www.geocities.com/sunjava_scjp
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

Please giving this link everyday on this forum.

This way we can remember to check the question everyday.

Thanks
Kaps
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

I am waiting for your next question of the day!!!

Kaps
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi krishan

the link is nice pls keep updating the questions so that we can learns things
thank you

san
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer given was correct but the explanation was wrong.

You can't refer to instance variables in the (coded or compiler-supplied) first line of a constructor because that line is executed before the instance exists. You can certainly refer to instance variables and methods in the rest of a constructor, confident that the object exists. In fact, a constructor is the last chance you get to initialize a blank final variable. All other variables will have already been initialized with default values or explicit initializers.
 
kapil munjal
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 is correct, we can't refer instance and final variables in the first line but we can call them from the second line.

Thanks Mike
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike, i tried to compile this code and it accesses/prints first line of constructor for variable.

class tst
{
public static void main (String st[])
{
new tst1();
}
}

class tst1{
int i = 10;
tst1(){
// First line of constructor accessing variable
System.out.println(i);
}
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac inserted "super();" as the first line of your constructor without telling you. So your print statement was the second line.

The first line of a constructor must be a "this( ... )" or "super( ... )" call. That's how the java establishes the sequence of constructors, starting with Object(), that will be executed to finish initializing your object. While these first lines are executing, the object hasn't been created, so they can't refer to instance methods or variables.

Once Object() has finished executing, the object exists, the instance variables have gotten default values, and the initializers have executed. Now the bodies of your constructors can run and access the instance members.
 
Krishna Srinivasan
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike is correct.
I made mistake. We cannot use instance reference veriable before calling super().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic