• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Can we have forward refrencing with final instance variable

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AnotherDirtyOne
{ private final int i =10;
private byte k = i;
}
An attempt to compile and run the above code will

1.Cause a compilation error due to invalid assignment ( int to byte) and will request for an explicit cast to be done on i [ k=(byte) i ].
2.Compilation occurs with a warning message - suggesting that the accuracy of k is questionable
3.Compilation will occur cleanly without any warning message.
4.Runtime error occurs when accessing k.

Ans: 3

Doubt: Why not 1. If i remove final, it gives compilation error. Also this is an example of forward referencing, so with final does forward referencing works?

pls help
regards,
gitesh
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/264749/java-programmer-SCJP/certification/Quote-your-sources
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, this does not have anything to do with forward referencing.

Java has a special way to handle assignments for the 'byte' data type. If you assign a compile-time constant value to a byte, then the compiler accepts it without a cast if the value of the constant is in the range for byte (-128 to 127). It will give you an error if the value is outside the range.

If you make 'i' final, then 'i' will become a compile-time constant, so the assignment without a cast is allowed.

Try changing it like this to see what happens:

private final int i = 150;

(Note: Please quote your sources).
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source:

AngelFire/Abhilash Q-29


When I give private final int i = 150;
i get compilation error as you mentioned.

test.java:3: possible loss of precision
found : int
required: byte
private byte k = i;
^
1 error

Thanks a lot for helping me out Jesper
 
Get me the mayor's office! I need to tell him about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic