The Artima Developer Community
Sponsored Link

Java Answers Forum
Very simple question I am sure

1 reply on 1 page. Most recent reply: Dec 2, 2005 9:12 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Very simple question I am sure Posted: Dec 2, 2005 1:01 PM
Reply to this message Reply
Advertisement
Why does my explicit call to super() in the TestSuper(int x, int y) not work? I am looking for the rule here please. It compiles and runs fine, but doesnt look like it does anything with it.

public class MyClass2 { 
         MyClass2(){ 
              System.out.println("This is the Super"); 
         } 
} 
 
public class TestSuper extends MyClass2{ 
         TestSuper(){ 
                     this(1,2); 
                     System.out.println("This is the Default"); 
         } 
         TestSuper(int x, int y){
                     super();
                     System.out.println("This is not the Super"); 
         } 
         public static void main(String[] args){ 
                     TestSuper ts = new TestSuper(); 
         } 
} 


Output:

This is the Super
This is not the Super
This is the Default

thank you


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Very simple question I am sure Posted: Dec 2, 2005 9:12 PM
Reply to this message Reply
Your call to super() is working as it is supposed to be. Here is the rule:

If a class inherits another class then the compiler put a call to super() (default consttuctor call of ancestor class) in all the constructors as the first statement if:
1. The first statement in the constructor is not the call to another construtor of the same class, or
2. The first statement in the constructor is not the call to any constructor of ancestor, that is, a statement like super() or super(args list)


The idea here is to call the constructor of ancestor before any code for the class's constructor is executed. Or, putting it another way, the idea is to construtor the object(or you may call it parts that belongs to ancestor) of ancestor before it starts constructing the object of desrived class.

So, in your case, when you call "new TestSuper()", then
1. you have first statement in TestSuper() as this(1,2), which has super() as its first statement, which is used to construct the ancestor part of object. Based on above argument, if you take out the call to super() from TestSuper(int x, int y) then compiler will put this call to super(). To verify this, comment out the call to super() and then you can see your output will be same.

Another idea is to construct an object only once. That is, you cannot call the constructor of ancestor twice by using super() twice or more in your code. This is the reason, compiler lets you put the call to super(...) only as the first statement in teh constructor to make its check easy and enforce the rule that ancestor part must be construted before derived part starts getting constructed.

Thanks
Kishori

Flat View: This topic has 1 reply on 1 page
Topic: Java NIO JVM memory problems Previous Topic   Next Topic Topic: Combo Values

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use