• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help , can any one explain the output ?  RSS feed

 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • 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 ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Henry i got your point !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!