• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

why doesn't this code run

Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this code 2 run the line marked with ******* has 2 b commented
//file Tester.java
class Q {
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
public class Tester extends Q {
Q sup = new Q();
Sub sub = new Sub(); //*******************this line has 2 b commented
public static void main(String args[])
new Tester().someMethod();
public void someMethod() {
// sup.privateVar = 1; // Line 10
sup.packageVar = 2; // Line 11
sup.protectedVar = 3; // Line 12
sup.publicVar = 4; // Line 13
// privateVar = 5; // Line 17
packageVar = 6; // Line 18
protectedVar = 7; // Line 19
publicVar = 8; // Line 20
Tester t = new Tester();
// t.privateVar = 9; // Line 25
t.packageVar = 10; // Line 26
t.protectedVar = 11; // Line 27
t.publicVar = 12; // Line 28

// sub.privateVar = 13; // Line 32
// sub.packageVar = 14; // Line 33
// sub.protectedVar = 15; // Line 34
// sub.publicVar = 16; // Line 35
class Sub extends Tester {}
Thanx in advance
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Leena,
Actually making the object of class Sub here makes infinite recursion, due to this you will get OutOfMemoryError. This is why it is happenning:-
When you call the line "Sub sub=new Sub()" it will call the constructor of Sub class. The constructor of Sub class is calling the constructor of Tester class. When the constructor of Tester class is called this line will execute again and this process will continue in infinite way.
I hope this will help you.
Gurpreet Sachdeva
For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Gurpreet :-)
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
Well the code does compile on my JDK!!
Now if the issue in here is whether its right to create an instance of the derived class during the process of creation of an object of the base class !! Well, its a bad design to do such a thing. Its a disaster! Why? Well, you as designer, design a class to represent a real life situation. Now,
the future requirement varrant that they inherent your the attribute/behaviour of your class plus some more additional
attributes/behavior. However, you as a designer have no knowledge as to how the future requirement change!!
Let me be elaborate, in the current context, in the code provided in this thread is being made to create an object of the derived object of the derived class during the creation process of the base class this leads to infinite calls to the base class's contructor. These calls are put on the stack of the memory and eventually your memory gets exausted and no more calls are possible, the JVM throws a stack overflow exception.
I hope this clears your doubts than confuse you.
[This message has been edited by Ravindra Mohan (edited October 03, 2001).]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!