• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic