File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need help on below code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need help on below code" Watch "Need help on below code" New topic

Need help on below code

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class X
X(String s)
public static void main(String[] args)
new X(null);
new Inherit("Dinesh",21);

class Inherit extends X
int i;

Inherit(String p,int x )


hi, hi, hello.
Why its printing two times hi.

Thanks<br />Dinesh
Abhishek Mantri

Joined: Sep 06, 2007
Posts: 25
Hi dinesh,
I think the code is working fine !!!

You get first "hi" since you have invoked X's constructor...agreed?

You get second "hi" since you have again invoked X's constructor...agreed?
check that irrespective of what String you send to X's constructor, it will only print "hi"

I think that solves your problem,rest you know!
good luck !!!
npk chowdary

Joined: Dec 09, 2007
Posts: 21
I am expecting that you are thinking about this code as why it is working even while passing null to string object.
here you are passing null means you are indirectly assigning string object to null.It leads to no problem in the constructor because you are not using String s for manipulation anywhere in your constructor.
I think it will clears .
yong huang

Joined: Jul 10, 2007
Posts: 7
I also learnt from this topic that below sentence A should be replaced with B.

A. The first line in a constructor Must be a call to super() or a call to this().

B. The first line in a constructor Must be a call to super() , or its overloaded version, or a call to this().
I agree. Here's the link:
subject: Need help on below code
It's not a secret anymore!