• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to mimic code similar to System.out.println() and getting syntax error  RSS feed

 
Vijay Kumar Raman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to call a method of a class for which I have created static final reference variable (not instantiating any new object) just like in System.out.println() as below,



The instance variable declaration line in CheckStaticObjRef .java class, "public static final StaticObjClass stob;" is giving compilation error "The blank final field may not have been initialized". I dont know where am missing something in my code.
Kindly help.

[Added code tags - see UseCodeTags]
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since stob is a final field, it has to be initialized.
 
Vijay Kumar Raman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see the System class, the reference variable out is not initialized
public final static PrintStream out = nullPrintStream();
where method call nullPrintStream() returns null

then how the out.println() method call works ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Kumar Raman wrote:If you see the System class, the reference variable out is not initialized
public final static PrintStream out = nullPrintStream();

Then it's been initialised, hasn't it? That's enough to keep the compiler quiet.

If you look more at the source code, you'll see that there's a native method that then gets called that will do some clever stuff and reassign out to the right value. It's a bit of a special case.
 
Vijay Kumar Raman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!