• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

From mock...

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Can someone explian me the ans for this code?
1. public class initialize
2 {
3 public initialize(int m)
4 {
5 System.out.println(i+","+j+","+k+","+x+","+m);
6 }
7 public static void main(String[]args)
8 {
9 initialize obj=new initialize(x);
10 }
11 static int i=5;
12 static int x;
13 int j=7;
14 int k;
15 {
16 j=70;x=20;
17 }
18 static
19 {
20 i=50;
21 }
22 }
Thanx,
Poonam.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Poonam Akash:
Hello,
Can someone explian me the ans for this code?
1. public class initialize
2 {
3 public initialize(int m)
4 {
5 System.out.println(i+","+j+","+k+","+x+","+m);
6 }
7 public static void main(String[]args)
8 {
9 initialize obj=new initialize(x);
10 }
11 static int i=5;
12 static int x;
13 int j=7;
14 int k;
15 {
16 j=70;x=20;
17 }
18 static
19 {
20 i=50;
21 }
22 }
Thanx,
Poonam.


Hai Poonam,
Here whats happening based on my understanding
We know static are class related so they get called first ie
very first during the class loading time, before even getting
into the OO perse, ie instantiation of objects.
So since our entry point here is main and being static, when it calls x, it has its default value. It wont be 20 since, its initalized in an instance initializer not a static initializer.
But at the same time, i becomes 50 is becoz its initialized in a
static intitalizer, which happens at the class load time
Hope that helps
PS: if you change the constructor's parameter in main to 'i' you will see that 50 occurs at both the end of the output
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i found the following in velmurugan's notes:
Order of code executin:
a) static variable initialization
b) static initializer block execution
c) constructor header (super or this - inplicit or explicit)
d) instance variabled initialization/instance initializer block (s)
e) rest of the code in the constructor.
hope this helps
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic