Solved

"cannot assign a value to final variable"

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now