Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"cannot assign a value to final variable"

Posted on 2004-05-01
4
Medium Priority
?
2,684 Views
Last Modified: 2012-06-22
i've got 2 classes:

public interface Constants {
  public static final int     MAX_LIGHTS = 10;
  public static final int     MAX_SOLIDS = 100;

  static Vector Eye         = new Vector ( 0, 0, 0 );         // camera position
  static Vector EyeDir      = new Vector ( 0, 0, 1 );         // viewing directing ( normal to image plane )
  static Vector Vx          = new Vector ( 1, 0, 0 );         // image plane basis - horizontal vector
  static Vector Vy          = new Vector ( 0, 1, 0 );         // vertical vector
}

public class Tracer implements Constants
{
  public Tracer () { }
 
  public static void SetCamera ( Vector Org, Vector Dir, Vector UpDir ) {
    Eye    = Org;                    // eye point
    EyeDir = Dir;                    // viewing direction
    Vx     = Vx.Normalize ( UpDir.Power ( UpDir, Dir ) );  // build orthogonal basis of image plane
    Vy     = Vy.Normalize ( Dir.Power ( Dir, Vx ) );    // EyeDir orthgonal to this basic ( image plane )
  }
}

when i try to compile it, it gives me this error:

D:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\ajajai\src\Tracer.java:35: cannot assign a value to final variable Eye
    Eye    = Org;                    // eye point

but why compiler claims that variable Eye is final? i'm sure it is not final, it's only static. where is the problem??
0
Comment
Question by:tigrine_smiltpele
  • 3
4 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 10967458
I thnk because it's already been defined in the interface that Tracer implements, which means to all intents that it was defined as a static member of Tracer, and now you are trying to redefine it.

The same compiler error would happen for example if you said "this=null;" inside a class constructor.
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10968100
>>but why compiler claims that variable Eye is final? i'm sure it is not final, it's only static. where is the problem??

fields defined inside a Interface are always "final" fields, and cannot be changed during runtime
0
 
LVL 7

Accepted Solution

by:
maheshexp earned 150 total points
ID: 10968115
so try using abstract class

public abstract class Constants {
  public static final int     MAX_LIGHTS = 10;
  public static final int     MAX_SOLIDS = 100;

  static Vector Eye         = new Vector ( 0, 0, 0 );         // camera position
  static Vector EyeDir      = new Vector ( 0, 0, 1 );         // viewing directing ( normal to image plane )
  static Vector Vx          = new Vector ( 1, 0, 0 );         // image plane basis - horizontal vector
  static Vector Vy          = new Vector ( 0, 1, 0 );         // vertical vector
}

public class Tracer extends Constants
{
  public Tracer () { }
 
  public static void SetCamera ( Vector Org, Vector Dir, Vector UpDir ) {
    Eye    = Org;                    // eye point
    EyeDir = Dir;                    // viewing direction
    Vx     = Vx.Normalize ( UpDir.Power ( UpDir, Dir ) );  // build orthogonal basis of image plane
    Vy     = Vy.Normalize ( Dir.Power ( Dir, Vx ) );    // EyeDir orthgonal to this basic ( image plane )
  }
}

hope it will work now
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10969796
thanks :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question