This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why doesn't this code run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why doesn Watch "why doesn New topic
Author

why doesn't this code run

leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
For this code 2 run the line marked with ******* has 2 b commented
why??
//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
Gurpreet Sachdeva
Ranch Hand

Joined: Feb 20, 2001
Posts: 90
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.
Regards
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


Regards<BR>Gurpreet Sachdeva<P>For Mock Exams, FAQ, Exam tips and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: <A HREF="http://www.go4java.lookscool.com" TARGET=_blank rel="nofollow">http://www.go4java.lookscool.com</A>
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
Thanx Gurpreet :-)
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
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).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why doesn't this code run
 
Similar Threads
Protected access
Question on Protected
Package & modifier question
question about member variable
Accessibility of Protected variables