aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The method output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The method output" Watch "The method output" New topic
Author

The method output

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

I don't know why the "0" gets printed here
please can anyone explain why is it?

The output is
0
4


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi prasad,
Java programs are starts it execution from the main method.
and
You override the method print of SuperClass in SubClass and also you call the method print in main method before the round method perform action.
In the print method you have print the value of i and
i is the global variable in your code
.So it automatically initialise with 0.So only it prints 0 and then 4.
Cheers Munees

Cheers Munees
My Blog
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

so does that mean that the print method gets called twice?
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

here I have modified some code and found that the method does not gets called twice
I used a counter there
and found something interesting
here is code


the output is
counter = 1
0
0
counter = 1
4
0


isn't it interesting?
let us discuss
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

and also
even if we do not use global variables in the method and just try to print out something something it still gives double output
what can be the fact?
here is the code
experts please answer why this is occuring

and output is
C:\Users\Prasad\Desktop>java SubClass
something something
something something
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

hahahaha
I am such a fool!
I think I got the solution
I will tell what I am thinking but please tell me whether it is right or not !
here is the code

and the output

C:\Users\Prasad\Desktop>java SubClass
Super class constructor running now
counter = 1
something something
0
0
Super class constructor completes here
counter = 2
something something
4
0

According to me following things happen
1. Starting of main method
2. on the object creation line, the subclass constructor runs and the default values are assigned to the variables of the subclass and also the superclass constructor runs due to the implicit call to the superclass no-arg constructor
3.in the super class constructor we are calling the method print hence the method gets executed and the default values are printed
4. Now as the superclass constructor has completed, the control returns back to the subclass and explicit values are assigned to the variables in the subclass
5. Now again we are calling the method using the object hence the explicit values are printed

Is it right now?

Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Ya,You are right prasad.
Cheers Munees
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The method output