• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

jtips ques  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Servlets {
Servlets(String s) {
System.out.println(s);
}
void writeLn(String s) {
System.out.println(s);
}
}
class Form {
static Servlets s1 = new Servlets("J as in Jam");
Form() {
s2.writeLn("V as in Victor");
}
void writeLn(String s) {
System.out.println(s);
}
static Servlets s2 = new Servlets("A as in Apple");
}
class JSP {
Servlets s3 = new Servlets("S as in Sam");
static Servlets s4 = new Servlets("A as in Apple");
JSP() {
s4.writeLn("P as in Pollard");
}
void writeLn(String s) {
System.out.println(s);
}
static Servlets s5 = new Servlets("J as in John");
}
public class Q52 {
public static void main(String[] args) {
System.out.println("Trimming J from JSP in main()...");
new JSP();
f.writeLn("A Simple Example of static Initialization !");
j.writeLn("A Simple Example of Instance Initialization !");
}
static Form f = new Form();
static JSP j = new JSP();
}
What is the Output?
1.Prints
Trimming J from JSP in main()�
S as in Sam
P as in Pollard
A Simple Example of static Initialization !
A Simple Example of Instance Initialization !
J as in Jam
A as in Apple
V as in Victor
A as in Apple
J as in John
S as in Sam
P as in Pollard
2.Prints
J as in Jam
A as in Apple
V as in Victor
A as in Apple
J as in John
S as in Sam
P as in Pollard
Trimming J from JSP in main()�
S as in Sam
P as in Pollard
A Simple Example of static Initialization !
A Simple Example of Instance Initialization !

3.Prints
Trimming J from JSP in main()�
S as in Sam
A as in Apple
J as in John
P as in Pollard
A Simple Example of static Initialization !
A Simple Example of Instance Initialization !
J as in Jam
A as in Apple
V as in Victor
S as in Sam
A as in Apple
J as in John
P as in Pollard
4.Prints
J as in Jam
A as in Apple
V as in Victor
A as in Apple
J as in John
S as in Sam
P as in Pollard
Trimming J from JSP in main()�
A as in Apple
J as in John
S as in Sam
P as in Pollard
A Simple Example of static Initialization !
A Simple Example of Instance Initialization !
the answer is 2 but i cant get it
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gaurav,
Lets take it one part at a time.
We start things off in the Q52 class. Static variables are initialized during class load. Therefore we start with the first line:
1. static Form f = new Form();
this causes the Form class to be loaded. The Form class has its own static variables that get initialized now:
2. static Servlets s1 = new Servlets("J as in Jam");
This line causes the Servlets class to be loaded and its constructor called. The Servlets class constructor has a print in it, therefore we get
J as in Jam
as the first output. The second static variable inside Form:
3. static Servlets s2 = new Servlets("A as in Apple");
causes the Servlets constructor to print out
A as in Apple
We are now done with the static variables inside the Form class (and no non-static ones exist) so we can go ahead and run the Form constructor to complete the line numbered 1 above. It also has a print inside of it so we get
V as in Victor
Now we are done with the first static variable inside Q52 we can move to the next one
4. static JSP j = new JSP();
This causes the JVM to load the JSP class. It has its' own static variables that must be initialized.
5. static Servlets s4 = new Servlets("A as in Apple");
This will run the Servlets constructor since it has already been loaded. The constructor will print
A as in Apple
The second static initialization can now be executed.
6. static Servlets s5 = new Servlets("J as in John");
Once again the constructor prints out:
J as in John
Now we have completed the static variable initialization we can go ahead and perform the non-static one:
7. Servlets s3 = new Servlets("S as in Sam");
This once again causes the print of
S as in Sam
We can now run the JSP constructor to complete the line numbered 4 above. The JSP constructor has a print that will output
P as in Pollard
We have now completed all the Q52 variable initializations so we can now go ahead and execute the main method. It starts out with a print
Trimming J from JSP in main()...
Moving to the next line inside main
8. new JSP();
This line now will make a new JSP object so the non-static variables need to be initialized first before we run the constructor. The line
9. Servlets s3 = new Servlets("S as in Sam");
will be executed and will print out the following line
S as in Sam
Now we can run the JSP constructor to complete the line numbered 8 above. It will print out:
P as in Pollard
We are now ready to run the fourth line inside the Q52 main method:
10. f.writeLn("A Simple Example of static Initialization !");
this will run the writeLn method for the Form object. It will print out
A Simple Example of static Initialization !
The next line to be executed will be
11. j.writeLn("A Simple Example of Instance Initialization !");
this will run the writeLn method for the JSP object. It will print out
A Simple Example of Instance Initialization !
This completes the execution of Q52 main method.
Regards,
Manfred.
 
gaurav nayyar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred ,
thanks for the explanation
Gaurav
 
A timing clock, fuse wire, high explosives and a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!