• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Static Initializer question

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi My friends:
I have one question from Sreenivasa Kumar Majji's Sun Certified Java programer practice exam exam 1, still don't understand. Please give me a help. Thanks.
The question is:
1: public class Q8
2: {
3: int i = 20;
4: static
5: {
6: int i = 10;
7:
8: }
9: public static void main(String[] args)
10: {
11: Q8 a = new Q8();
12: System.out.println(a.i);
13: }
14: }
A) Compilation error, variable "i" declared twice.
B) Compilation error, static initializers for initialization purpose only.
C) Prints 10.
D) Prints 20.
the answer is D.
Another question is:
if a variable is defined and initialized in the class, and the same variable is defined and initialized in a method in the same class. What is the result if I print this variable in the local method or in the class scope?
Haijun
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is -you have some static code within the class Q8, which is run when the class is loaded. However, the variable i inside this static block is local to this block and hence goes out of scope after the code block ends. It does not affect class variable i.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
You have two different variables here but they have the same identifiers. The scope is different for both. The static variable belongs to the whole class hence if you are refering to a.i, you are actually refering to the non static variable and hence 20 gets printed. Try putting in this stmt in the static block: System.out.println(i) and you'll see 10 printed out. This is because any static code block is executed by the JVM when the class is loaded, even before any objects are instantiated and before main() runs.
HTH
Shashi
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this site.It has a wonderful explanation of Static Initializer.
http://javaranch.com/maha/Discussions/Declaration_And_Access_Control/final_variable_-_JavaRanch_Big_Moose_Saloon.htm
 
haijun wang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you every one who gives me the detailed explanation for this basic questions.
Have a good day.
Thank you from deep heart.
Haijun
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Clear one thing in mind that anything declared within the static block is not static. The thing is that it is initialized before the main() method. So, here the variable is not static and you can have more then one copy of this variable. So, here first i is assigned value of 10 and after that its changed to 20. and hence the answer.
The answer to the second question is whenever you print the value of any variable the last value will be printed. So, you have to go line-by-line. And detemine the last value. Don't be confused with scope and all that thing.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sasikanth,
This
System.out.println("i : " + i);
statment never works if given in a static block or static method. The compiler cribs saying that
C:\Java\bin>javac T.java
T.java:40: Can't make a static reference to nonstatic variable i in class Q8. System.out.println("i : " + i);
^
The value of i which is an instance varible is 20.
remember the order of execution......
static variables/blocks, contructor, instance variables/blocks.
So here first the
static {
int i = 10;
}
gets executed. i = 10.
after the contructor the instance variables/ block will be assigned or executed. Then a new int i with value 20 is assigned.
HTH,
Aruna
 
reply
    Bookmark Topic Watch Topic
  • New Topic