Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

plz help me

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
why this program cause a compiler error?
class helloworld1{
int i;
helloworld1(){
this(i);
System.out.println("default");
}
helloworld1(int y){
System.out.println("with argu");
}
public static void main(String args[]){
new helloworld1();
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for starters, constructors need an access modifier. In this case, "public" should do nicely.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since in your no-argument construtor you used your first line to call the int arg constructor, the compiler puts the call to the super constructor (in this case Object) in the first line of the int arg constructor.
To construct the instance, first the JVM allocates memory for all the variables required, both in the instance and in all of it's super classes. Then the variables are initialized. Then you can use them.
In your case, you needed the value of i to be used in order to get to the next constructor, where the call to the super could be made to FIND the variables that it would require.
Try replacing the this(i) call with this(5); or something and it will work.


12.5 Creation of New Class Instances
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (�8.3). If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (�4.5.5).

 
reehan ishaque
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well cindy
i appreciate ur effort to make me understand. but im still not pretty clear abt it. does it mean tat we cant pass uninitialized instance variable as a parameter to functions or constructers?
again thx for ur effort
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, basically that is it. It means that you can not use an instance variable of the same class to get to another constructor, either in this class or in a super class, BECAUSE it will not be initialized at that point.
However:

This works because the static variable is resolved at load time, not at construction time.
[This message has been edited by Cindy Glass (edited May 18, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic