• 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

Two questions

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two questions:
Q1)class Fruit
{
static Fruit f = new Fruit();//*I
Fruit g; //*II
Fruit h = new Fruit();//*III
}
class Tomato
{
public static void main(String args[])
{
Fruit f = new Fruit();//*IV
}
}
In 1) if i include *III and *IV lines an error comes
like this an errror comes like this:
Exception in thread "main" java.lang.StackOverFlowError
at Fruit.<init>(Compiled Code)
at Fruit.<init>(Compiled Code)
at Fruit.<init>(Compiled Code)
at Fruit.<clinit>(Tomato.java:3)
Why so and what this means?
---------------------------
In 1) if I exclude *III and *IV no errors.
In 1) if I exclude *IV and include *I,*II,*III
no such errors.How?
-------------------------------------------------------------------------
-----------------------------------------------------------------------
Q2)At what time static methods get executed-compile
time or at runtime (with initialization)?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer for ur first question.
consider the case :u include only line 1 &4 -----------------
what happens at the line4.u r creating an instance of class Fruit.at that time, instance variables of class Fruit are initialised.here a new fruit object is created at line 1.this instance creation again cause instance variable in the fruit class to initialised.this process continues until stack is overflowed.the creation of fruit object takes place continuously until stack overflows. that is why u get StackOverFlowException
hope this helps.
kinnu
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jyothi
I think your problem is because each instance of your class contains a variable that refers to another instance of the same class. This is created in Line III, Line I is not too bad as it is static so it will only be created one time when the class is first loaded. Line II doesn't actually allocate memory so it is not an issue.
In line IV when you create an instance of Fruit it calls the default constructor, then initializes all of the instance variables. Because one of the instance variables is another instance of Fruit it creates another Fruit object, but while it is doig that it has to create another one and so on, until you overflow the stack. It is almost like recursively calling the constructor for the Fruit class.
If you exclude III and IV there are no errors because you never actually create a Fruit object, so the constructor isn't called. The same applies to just excluding IV you never create an instance of Fruit so the constructor is never called.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jyothi abraham:
Q2)At what time static methods get executed-compile
time or at runtime (with initialization)?


Sorry I forgot your second question
Do you mean when are they bound compile time or runtime? Because static metods can not be overridden - just hidden- they are bound at compile time and are called based on the type of the variable that is calling them not the underlying type of the object the variable refers to.


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the others have explained, if you exclude line IV, then no object of type fruit is ever instantiated. The problem, of course, is on line III. Every time you create an object of type Fruit, it creates an additional object of type Fruit at line III. The additional object creates yet another object of type Fruit at line III, which creates yet another object of type Fruit at line III, which creates yet another object of type Fruit at line III, and so on. Eventually, you run out of memory.
The more intersting case is when you include the static instance at line I. When you create the Fruit object in class Tomato, the first thing that happens is static initialization. At line I, a Fruit object is created during static initialization. Apparently, static initization is interupted while a single Fruit object is created and a reference to it is assigned to f. Then static initialization resumes and ultimately a second Fruit object is created and a reference to it is assigned to f in Tomato.main.
This is illustrated in the following code:

The output is:
i = 0
i = 5
In main, i = 3
false

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic