Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Access modifier

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output:H.printS1,null
why null ?!
and if you put static in line 1 and 2 you will get
H.s1?
thanks in advs.
tony
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code has already the static modifier. But if it doesn't have the static modifier yet, then:

In case of no static modifier:
1- At line 3 you inistanciate a new Hm Object, Hm extends G, so the Hm() constructor implicitly calls G(), G() at line 1 calls the method printS1().
2- Since the Object is of run-time type Hm and printS1 is an instance method, overriden in the subclass Hm, the method printS1() at line 2 is the one that is invoked (polymorphyism).
3- printS1() tries to use S1 which was not assigned its value yet but had a default value (null) as the constructor of Hm is not executed yet - we're still working on the G() constructor - and thus it prints null.
in case of static, put in mind that static initializers and blocks are executed directly after the JVM loads the class, so in the case of static, the S1 variable in Hm class is assigned its value before any of these 3 steps take place, thus it prints Hm.S1.
Note that even if you don't declare the S1 in class G static, and only declared the one in Hm static, it will still print Hm.s1 cause the method at line 3 uses S1 of Hm.
HTH
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alfred,
I modified the code a little bit

Output:
H.print,null
H.printS1,G.s1
When I followed your explanation, I thought that when G's constructor gets called, it calls the println(String s) in Hm - these 2 steps seems right, but the weird thing is println(String s) in Hm uses the G's s1. I couldn't understand why.
TIA.
:roll:
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yan,
This from JLS 15.12.4


At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. Third, the accessibility of the method to be invoked is checked. Fourth, the actual code for the method to be executed is located. Fifth, a new activation frame is created, synchronization is performed if necessary, and control is transferred to the method code.


Regarding printS1(s1) in G constructor:
Notice that the evaluation of the arguments (s1) occurs in the second step, before the actual location of the code to be called is performed. That is: the string "G.s1" is passed (a reference to it is stored in the operand stack) and that value will be the one used by the code finally invoked.
[ November 03, 2002: Message edited by: Jose Botella ]
[ November 03, 2002: Message edited by: Jose Botella ]
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella

Regarding printS1(s1) in G constructor:
Notice that the evaluation of the arguments (s1) occurs in the second step, before the actual location of the code to be called is performed. That is: the string "G.s1" is passed (a reference to it is stored in the operand stack) and that value will be the one used by the code finally invoked.


So I will be more cautious with those methods having argument lists.
Thanks, Jose.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, definitely, sometimes some degree of caution is needed with those arguments lists.
JLS 15.7.4
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic