• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification on the answer given

 
kc pradeep
Greenhorn
Posts: 29
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output for the below code ?



Options are
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error - static variable must be initialized before use.
Answer :
A is the correct answer.
Global and static variable need not be initialized before use. Default value of global and
static int variable is zero. Default value of boolean variable is false. Remember local
variable must be initialized before use.

My question is can printValue() access static variable p?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kc pradeep wrote:What is the output for the below code ?



Options are
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error - static variable must be initialized before use.
Answer :
A is the correct answer.
Global and static variable need not be initialized before use. Default value of global and
static int variable is zero. Default value of boolean variable is false. Remember local
variable must be initialized before use.

My question is can printValue() access static variable p?


why can't it ? what are your reasons that printValue() cannot access static variable p ?
 
kc pradeep
Greenhorn
Posts: 29
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops sorry, I got confused with the other rule : static functions can only access static members.
yeah, non static functions can access both, right.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kc pradeep wrote:oops sorry, I got confused with the other rule : static functions can only access static members.
yeah, non static functions can access both, right.


you got that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic