• 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
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

regarding forward referencing and 'implicit narrowing conversion'

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have 3 questions
1. at line 7, s1 is NOT available at "static scope" yet, why does assignment statement valid?
2. why does s1 become 0 after line 11?
3. does forward referencing works only for assignment statement (varaible as LHS of assignment) inside static/instance initializer? or valid for any other situation?
-------------------------------------------------------------------------------

Every integer literals are int by default, right? So are there any rules governing those 'automatic narrowing conversion' for integer family? It bangs my head each time I read similar questions and make me become paranoid that I even doubt whether is a valid statement
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your first question clarification read JLS 8.3.2.3 Restrictions on the use of Fields during Initialization.
About your second question,
You are right that every integer literal is int by default. Thats why
byte b1 = (int) 100 ;
is same as
byte b1 = 100;
but
int i = 100;
byte b1 = i; // is not allowed unless you do explicit casting like
byte b1 = (byte) i ; // because i is not literal and its value can change runtime.
Also, statment like
byte b2 = (long)0; // will always give compilation error for reasone stated above
// 0 is by default int literal.
I hope this helps
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are good questions. For the 2nd question followings are my assumtions . Tell me if I am right or wrong.
1.Only the integer literals can be implicitly cast to the narrowing type byte, short, char.
So
byte b = 100 ;//legal
byte bb = 1L; //illegal, long literal
byte b1 = (long)0; //illegal as 0 will be a long literal ater the cast
byte b2 = (short)10; //illegal RHS is not an int

2. Constant expressions whoch vaue is evalauated to integer are also implicitly cast to narrowing type.
byte b3 = 1+1 ;//legal ,evaluated to int.
byte b4 = 1L + 1; //illegal, evaluated to long
Thanks,
Ambapali
 
Aaron Anders
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compiles without error
my question is: are there ANY practical usage of such a language construct? besides tricking exam-takers

BTW, both JBuilder8 and Eclipse 2.0.2 compile with error "Cannot reference a field before it is defined"
[ January 01, 2003: Message edited by: Aaron Anders ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.
class Weird
{ static
{ int x = 100;
}
static int x;
}
You will compile without any problem. This is because you haven't define the int type of the variable x in the static block.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I tried the following code on my m/c I am getting compiler errors:
illegal forward reference xx=100;
[code]
static{
xx=100;
xy=200;
}
static int xx;
static int xy;
[code]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarma,
Please check again. I tried the following piece of code and it compile fine :
class staticInitializer {
static{
xx=100;
xy=200;
}
static int xx;
static int xy;
}
 
Anup Katariya
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting error for above code.
error is
" illegal forward reference".
I tried it on jdk1.2 and jdk1.4
Thanks
 
Aaron Anders
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anup Katariya:
I am getting error for above code.
error is
" illegal forward reference".
I tried it on jdk1.2 and jdk1.4
Thanks


as I stated above, it compiles successfully on JDK 1.4.1_01 but failed on JBuilder8 and Eclipse 2.0.2
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, Are you sure that the following code:

compiles without error!?
I am using JDK1.2 and it gives a compler error:
Can't make forward reference to i in class Test.
What is happening? Please help me.
[ January 02, 2003: Message edited by: Prosenjit Banerjee ]
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody please answer me.
 
Aaron Anders
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prosenjit Banerjee:
Anybody please answer me.


IMHO you may download JDK 1.4.1 and see it for yourself...
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code compiles fine with Sun's J2SE 1.4.1
See the last example given in JLS 8.3.2.3 Restrictions on the use of Fields during Initialization. On the first line of the first static block, you can see that the variable x is assigned the value 100 even though x is declared later.
The weird thing is that as per the JLS, the code should not compile.
Check out bugs 4281572 and4459133
One of Sun's developers said:


javac simply doesn't yet implement the second bullet of 8.3.2.3.


This might explain the issue...
 
Aaron Anders
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
The code compiles fine with Sun's J2SE 1.4.1
See the last example given in JLS 8.3.2.3 Restrictions on the use of Fields during Initialization. On the first line of the first static block, you can see that the variable x is assigned the value 100 even though x is declared later.
The weird thing is that as per the JLS, the code should not compile.
Check out bugs 4281572 and4459133
One of Sun's developers said:

This might explain the issue...


that's VERY weird, because I think JDK should pass all "JLS-conformance test" before official release...
also, what should we answer if the code above appears in exam? (compile successfully or fail?)
[ January 03, 2003: Message edited by: Aaron Anders ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's VERY weird, because I think JDK should pass all "JLS-conformance test" before official release...
Anybody who has taken the time to browse through Sun's Bug database knows that this is not the case
also, what should we answer if the code above appears in exam? (compile successfully or fail?)
This is not going to happen
 
Aaron Anders
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
that's VERY weird, because I think JDK should pass all "JLS-conformance test" before official release...
Anybody who has taken the time to browse through Sun's Bug database knows that this is not the case
also, what should we answer if the code above appears in exam? (compile successfully or fail?)
This is not going to happen


thanks for your clarification!
 
reply
    Bookmark Topic Watch Topic
  • New Topic