Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Last Day, Killers Questions for the experts only !

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1-
class Test {
static String s;
static {
s = "s initialized by static initializer";
System.out.println(s);
}
Test() {
s = "s initialized by constructor";
System.out.println(s);
}
public static void main(String[] args) {
System.out.println(s);
}
}
Debugging this code I got "s initialized by static initializer";
twice ! Why didn't call the constructor assigned ???
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2-
class Base {
Base(String s) {
System.out.println("s of Test: " + s);
}
}
class Test extends Base {
String s = "Original string of Test";
{
s = "s initialized by Instance initializer of Test";
System.out.println(s);
}
Test() {
super(s);
s = "s initialized by constructor of Test";
System.out.println(s);
}
public static void main(String[] args) {
Test t = new Test();
}
}
Compiled got an error in Super(s) !!! But it's supose to call the string version of the contruct of the Base and execute "System.out.println("s of Test: " + s);"
Someone can tell me WHY ???
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Thx in advance !
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your second question:
In your call super(s), you are trying to use an object's instance variable (String s) before the object has been fully created. Make s static in class Test and it'll work!
//Kaspar
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for your first question, I don't get it. It compiles fine, and when you run it, it prints
s initialized by static initializer
s initialized by static initializer
This is good, isn't it? When a class is loaded, its static initializers are run before the main method starts. So there one s gets printed. And you print it another time in the main method...
Good luck on the test! Just take it easy, and it'll be sooo smooooth....
//Kaspar
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i'm no "expert" ... but i think the answer to the first one is rather simple
the class has not been instantiated in the code and hence it prints .... modify your code to add one line and see the result
.... this is also the very reason why the main is static ... so that the compiler does not require an object of the class to execute main
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops.. Sorry for spamming...
The reason your constructor isn't called is because you don't create an object...
/Kaspar
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1-
You never created an instance of Test. In other words, you never called Test's constructor. So why should the constructor be invoked? The String "s initialized by static initializer" was first displayed when the class was first loaded (when invoking main). Then, in main, you displayed s again when s's value was still "s initialized by static initializer".
2-
This is a case of forward referencing, using a reference before it was created. When you used s in super(s), s was not yet initialized; like Kaspar said, make s static will allow s to be initialized at loading time instead of instantiation time. And it will no longer be forward referencing.
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there Ricardo! great example you have here! at first glance i also thought that this piece of code will compile but i was wrong.

i guess we got so used to examples where the main method passes variables to the constructors that we almost left out this scenario where an object which hasn't been created tries to pass one of its instance variables as a parameter to its parent's constructor.
 
reply
    Bookmark Topic Watch Topic
  • New Topic