• 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

Jaworski Mock Exam

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please explain
class Start{
static int i =1, j=2;
static {
display(i);
}
public static void main (String[]args){
display(j);
}
static void display(int n){
System.out.print(n);
}
}
When you compile this 12 is the Answer. Please clarify how it works out.
Thanks

------------------
Ch. Vijayalakshmi
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Once the program gets executed, the first thing it does is execute any initializer code blocks, it does the same here, first it goes into the static initializer block of code and calls display() with i=1 and then it calls the display() in the main method.
Hope this helps
 
Jay S
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give me the site adress for this mock exam??
Thanks
 
Vijayalakshmi Chipada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay,
Can you explain how you will get 12 .
www.jaworski.com/java/certification
Thanks

------------------
Ch. Vijayalakshmi
 
Jay S
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Check this out:
class Start{
static int i =1, j=2; // First step
//Second step
static {
display(i);
}
//Third step
public static void main (String[]args){
display(j);
}
static void display(int n){
System.out.print("\nvalue of n is " + n);
}
}

Now as I said when your class is loaded it follows the way I said in the above code.
First step: it initializes the static vars.
Second step: it executes the static code initializers (if any)
It may also execute any non static initializer codes (if any) in the order they appear in your code from top to bottom.
So in your 2nd step it calls the display method with i=1 since "i" is initialized a 1 in first step. It prints out the value of "i" i.e., 1
Third step: Then it calls the display method in your main method with j=2 as its parameter which inturn prints j value in that method.

thus you get 12 in your code.
Try to change the System.out line as I did and run. It'l be clear for you.
Hope you got it.
anyways Thanks for the link.
jay
 
Vijayalakshmi Chipada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your explanation


------------------
Ch. Vijayalakshmi
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi VijayaLakshmi,
jay u made a mistsake non-static initializers wont be called at class load time in this program it wont be called non-static initialixers are called only when an object is created
when a class is loaded all the static initializers are executed in the order in which they are defined
In the program given by u the static initializer is called first which will print 1 then main calls the display and will print 2 note that print is called not println
Cherry
[This message has been edited by Cherry Mathew (edited January 12, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic