• 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

constructor & toString

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,
Pl let me know what is happening in the below code
class Test{
int i=6;
Test(){
//this(i);
System.out.println(i);//this is working how?
}
Test(int j){
System.out.println(j);
}
public static void main(String []args){
Test t=new Test();
}
}
here when I called this(i) from the constructor there is a compile time error becoz i is not intialized yet..that I understood.
But then why println statement with reference i in the constructor is giving value 0???
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rishi,
Just the opinion of another greenhorn, so hopefully somebody will chime in if this is wrong, but I think this is what's happening:
You can only get access to your class's instance variables once the superclass constructor runs. When you have the call to this in your constructor, the compiler can't insert its no-arg call to the superclass constructor, so your class's instance variables aren't available yet. Remember you can't have BOTH this and super calls in the same constructor. When you take the this call out, now the compiler can insert that super constructor call, and when it returns from that call, your program can get at its instance variables which are initialized to 0.
Kathy
 
Kathy Hodgson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rishi,
Just the opinion of another greenhorn, so hopefully somebody will chime in if this is wrong, but I think this is what's happening:
You can only get access to your class's instance variables once the superclass constructor runs. When you have the call to this in your constructor, the compiler can't insert its no-arg call to the superclass constructor, so your class's instance variables aren't available yet. Remember you can't have BOTH this and super calls in the same constructor. When you take the this call out, now the compiler can insert that super constructor call, and when it returns from that call, your program can get at its instance variables which are initialized to 0.
Kathy
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Kathy u r right...I got the point now
Thanx
rishi
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rishi Wright:
Hi All ,
Pl let me know what is happening in the below code
class Test{
int i=6;
Test(){
//this(i);
System.out.println(i);//this is working how?
}
Test(int j){
System.out.println(j);
}
public static void main(String []args){
Test t=new Test();
}
}
here when I called this(i) from the constructor there is a compile time error becoz i is not intialized yet..that I understood.
But then why println statement with reference i in the constructor is giving value 0???


I am getting 6 as the output. Why are you getting 0 ?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Language Specification 8.8.5.1


An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.


So, in your case, juste make i static, and no more compiler error will be generated:

output:
6
6
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..'ll
Just read That
quote:
----------------------------------------------------------------------------
Constructors can throw exceptions. If parent default constructor throw exception, subclass have to do the same or have to handle the execption
----------------------------------------------------------------------------
means
Is it possible to catch the exception thrown by superclass constructor in subclass constructor...as super/this() has to be the first statement in subclass constructor
rishi??
 
reply
    Bookmark Topic Watch Topic
  • New Topic