Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could you change the static value declaration?(scjp07-95))

 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q95 Which of the following are true about this variable declaration?
private static int i = 3;
A The value of i may not be changed after it is assigned a value.
B i may only be updated by a static method.
C The value of i is shared among all instances of the class in which it is declared.
D i may only be accessed within the class in which it is declared.
the given ans is;CD.
But I think the :BCD.
Pls correct me if I am wrong.you always welcome the item.
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gong James:
Q95 Which of the following are true about this variable declaration?
private static int i = 3;
A The value of i may not be changed after it is assigned a value.
B i may only be updated by a static method.
C The value of i is shared among all instances of the class in which it is declared.
D i may only be accessed within the class in which it is declared.
the given ans is;CD.
But I think the :BCD.
Pls correct me if I am wrong.you always welcome the item.

Hi Gong James
THe static keyword before var i informs that var i is shared by all intances of the class not showing where var i can be modified.
Hope this help
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of static member variable can be modified in any static/instance intializer block and/or static or non-static method, and constructor.
Here is the demo:
========================================================
public class Reply
{
static int i = 10;
public static void main(String args[]) {
Reply r = new Reply();
r.try1();
try2();
System.out.println(i);
}
static
{
i = 20;
System.out.println("The value of i is : " + i);
}
{
i = 30;
System.out.println("The value of i is : " + i);
}
Reply()
{
i = 40;
System.out.println("The value of i is : " + i);
}
void try1(){
i = 50;
System.out.println("The value of i is : " + i);
}
static void try2(){
i = 60;
System.out.println("The value of i is : " + i);
}
}
========================================================
HTH
--Farooq
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't catch what you mean,you mean the B is right or wrong ?
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the ans B is wrong .This had been proved right.
thank you very much,I agree with you.
Thank you very much!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic