File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The method output

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why the "0" gets printed here
please can anyone explain why is it?

The output is
0
4
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so does that mean that the print method gets called twice?
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya,You are right prasad.
Cheers Munees
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic