Solved

"cannot assign a value to final variable"

Posted on 2004-05-01
4
2,650 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 16

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 50 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

856 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