File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block and constructor problem

 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source Certpal mock test




the answers given are
10
11
12
14
compile error


answer is 11
but why not 12
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how 12?
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you instatiate cert you pass 10 and it calls a super constructor but super constructor does not save the increased value anywhere, and when x++ is executed in cert it has value 10 so after the statement it becomes 11.
and if you call super.x it will print 10 original value of x in super class.
 
rohan yadav
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are calling Grade(int x) constructor and incrementing x value i.e x++, means you are increasing local variables value, not the class's static variable's value(remember that concept of shadowing is applied here).
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup got it rohan
thanks to neha and raju also
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic