Solved

"cannot assign a value to final variable"

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 57
advertisement module in core php 4 145
Spring Framework HTTPSession management 1 23
session migration servlets 2 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

920 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

12 Experts available now in Live!

Get 1:1 Help Now