Solved

"cannot assign a value to final variable"

Posted on 2004-05-01
4
2,654 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

756 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