Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Help , can any one explain the output ?

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have written following pieces of codes written in different files:-

--------------------------------------------------------------
AbstractClassC.java
--------------------------------------------------------------

--------------------------------------------------------------

AND

--------------------------------------------------------------
DeclarationsAndAccessControlModifiersPart1.java
--------------------------------------------------------------

// ------------------------------------------------------------------------------------------------


--------------------------------------------------------------


When the above file is compiled and run then i get following output :-

The value of i is 5
The value of k is 3
--------------------------------------------------------------
The Value of ClassA.object.publicInstanceVariableA is 0
The Value of ClassA.object.privateInstanceVariableB is 0
The Value of ClassA.object.protectedInstanceVariableC is 0


The Value of ClassA.object.publicInstanceVariableA is 1
The Value of ClassA.object.privateInstanceVariableB is 0
The Value of ClassA.object.protectedInstanceVariableC is 1


--------------------------------------------------------------
The value of k is 3
The Value of ClassA.object.publicInstanceVariableA is 2
The Value of ClassA.object.privateInstanceVariableB is 3
The Value of ClassA.object.protectedInstanceVariableC is 4


The Value of ClassA.object.publicInstanceVariableA is 3
The Value of ClassA.object.privateInstanceVariableB is 3
The Value of ClassA.object.protectedInstanceVariableC is 5


--------------------------------------------------------------
The value of j is 5
The value of k is 3
The Value of ClassB.object.publicInstanceVariableA is 0
The Value of ClassB.object.privateInstanceVariableB is 0
The Value of ClassB.object.protectedInstanceVariableC is 0


The Value of ClassB.object.publicInstanceVariable1 is 0
The Value of ClassB.object.privateInstanceVariable2 is 0
The Value of ClassB.object.protectedInstanceVariable3 is 0


The Value of ClassB.object.publicInstanceVariableA is -1
The Value of ClassB.object.privateInstanceVariableB is 0
The Value of ClassB.object.protectedInstanceVariableC is -1


The Value of ClassB.object.publicInstanceVariable1 is -1
The Value of ClassB.object.privateInstanceVariable2 is 0
The Value of ClassB.object.protectedInstanceVariable3 is -1


--------------------------------------------------------------
--------------------------------------------------------------
--------------------------------------------------------------
The value of k is 3
The value of publicVariableInAbstractClassC in the abstract class is 1035
The value of privateVariableInAbstractClassC in the abstract class is 3060
The value of protectedVariableInAbstractClassC in the abstract class is 1564
The value of publicVariableInAbstractClassC in the abstract class is 99
The value of privateVariableInAbstractClassC in the abstract class is 3060
The value of protectedVariableInAbstractClassC in the abstract class is 1564
--------------------------------------------------------------


From the output i am not getting when and how is the value of "k" in the AbstractClassC printed ? If the block of code containing the value of k in AbstractClassC is made static then the output is :-

The value of k is 3
The value of i is 5
--------------------------------------------------------------
The Value of ClassA.object.publicInstanceVariableA is 0
The Value of ClassA.object.privateInstanceVariableB is 0
The Value of ClassA.object.protectedInstanceVariableC is 0


The Value of ClassA.object.publicInstanceVariableA is 1
The Value of ClassA.object.privateInstanceVariableB is 0
The Value of ClassA.object.protectedInstanceVariableC is 1


--------------------------------------------------------------
The Value of ClassA.object.publicInstanceVariableA is 2
The Value of ClassA.object.privateInstanceVariableB is 3
The Value of ClassA.object.protectedInstanceVariableC is 4


The Value of ClassA.object.publicInstanceVariableA is 3
The Value of ClassA.object.privateInstanceVariableB is 3
The Value of ClassA.object.protectedInstanceVariableC is 5


--------------------------------------------------------------
The value of j is 5
The Value of ClassB.object.publicInstanceVariableA is 0
The Value of ClassB.object.privateInstanceVariableB is 0
The Value of ClassB.object.protectedInstanceVariableC is 0


The Value of ClassB.object.publicInstanceVariable1 is 0
The Value of ClassB.object.privateInstanceVariable2 is 0
The Value of ClassB.object.protectedInstanceVariable3 is 0


The Value of ClassB.object.publicInstanceVariableA is -1
The Value of ClassB.object.privateInstanceVariableB is 0
The Value of ClassB.object.protectedInstanceVariableC is -1


The Value of ClassB.object.publicInstanceVariable1 is -1
The Value of ClassB.object.privateInstanceVariable2 is 0
The Value of ClassB.object.protectedInstanceVariable3 is -1


--------------------------------------------------------------
--------------------------------------------------------------
--------------------------------------------------------------
The value of publicVariableInAbstractClassC in the abstract class is 1035
The value of privateVariableInAbstractClassC in the abstract class is 3060
The value of protectedVariableInAbstractClassC in the abstract class is 1564
The value of publicVariableInAbstractClassC in the abstract class is 99
The value of privateVariableInAbstractClassC in the abstract class is 3060
The value of protectedVariableInAbstractClassC in the abstract class is 1564
--------------------------------------------------------------

Why and how are the two outputs different as far as the display of "k" is concerned ?
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From the output i am not getting when and how is the value of "k" in the AbstractClassC printed ? If the block of code containing the value of k in AbstractClassC is made static then the output is :-



That "block of code" is called an instance initializer. During construction, after the super class constructor is called, but before the rest of the constructor is executed... All instance variables are initialized, and all instance initializers are executed, in order from top to bottom as defined in the class.

If the "static" keyword is added, that "block of code" is called a static initializer. When a class is loaded, all static variables are initialized, and static initializers are executed, in order from top to bottom as definied in the class.

Henry
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry i got your point !
reply
    Bookmark Topic Watch Topic
  • New Topic