• 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Initialization

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sample code from Khalid A.mughal book.
class Initialization {
private String msg(String mg){
System.out.println(mg);
return mg;
}
{
s=msg("2");
}
String s=msg("1");
public static void main(String ar[]){
Initialization i = new Initialization();
}
}

the book says that initializer doesn't support forward refrencing.
but when I complie and run the code on my machine, it works and print 2 followed by 1.
anyone can help..???

thanks in advance...
[ February 01, 2003: Message edited by: toto huang ]
[ February 01, 2003: Message edited by: toto huang ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I tried executing the same program on my machine.it generated a compilation error saying:
Cant make forward reference to s in class initialization.
 
toto huang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I'm using j2sdk1.4.1_01 to compile...
but anyway, I just take it as complie error... as the book say it so.
Thanks praveena...
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch toto.
The example compiles ok because the use of s is on the left hand of the assignment. Try placing String t = s; after s = msg("2") and the compiler will complain. The reason is explained in JLS 8.3.2.3 Restrictions on the use of Fields during Initialization
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jose,
Can you explain it in more detail.
JLS says that declaration of a member needs to appear before it is used only if it is an instance field of a class or interface.
So here s is the member and it is used to call the method msg even before it is declared.
But what doed it mean when JLS says usage is not on the left hand side of the arguement.
Thank you
Pallavi
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


8.3.2.3 Restrictions on the use of Fields during Initialization
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
* The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
* The usage is not on the left hand side of an assignment.
* C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the three requirements above are not met.



It compiles ok. "s" is in the left hand side of an assignment. The use of "s" occurs within an instance (block) initializer.

Compiler error, forwarding reference is not allowed because "s" is on the right of the assignment.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic