简体   繁体   中英

Eclipse debug mode changing variables

When programming in Java using the Eclipse IDE, I sometimes use the debug function. Quite a lot of the time I like to change variables in real time, whilst the programs are running. However, quite often I find that changing some variables won't actually affect the currently running program.

My question is: are there certain rules for debugging? which variables are in scope to the debugger or something?

I'm sorry if this is a stupid question. I'm fairly new to debugging in Eclipse, and programming in general.

The code below is a sample. I'm sorry if it's hard to read or whatever but here's the issue: In the Ball class, whenever I alter the final variables such as PARTICLES_PER_CLICK or SPEED , they are updated in real time and I can see the difference in the program window, however when I alter the RADIUS variable, it does nothing, even though it's in the same class as the other two variables.

public class Main {

    public static final int WIDTH = 1280;
    public static final int HEIGHT = 720;
    public static Ball[] ball = new Ball[100000];
    public Main() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
            Display.setTitle("Game Engine");

        } catch (LWJGLException e) {

            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);


        boolean[] doOnce = new boolean[10];
        boolean gravity = false;
        while (!Display.isCloseRequested()) {

            if (Mouse.isButtonDown(1) || Mouse.isButtonDown(1)) {
                if (!doOnce[0]) {
                    Ball.createParticles();
                    doOnce[0]=true;
                }

            } else {
                doOnce[0] = false;
            }

            glClear(GL_COLOR_BUFFER_BIT);

            for (int i = 1; i <= Ball.ballAmount; i++) {
                ball[i].updatePosition(gravity);
                if (Mouse.isButtonDown(0)) {
                    gravity = true;
                } else {
                    gravity = false;
                }

                if (ball[i].position.x > 0 && ball[i].position.y > 0 && ball[i].position.x < WIDTH && ball[i].position.y < HEIGHT) {
                    glBegin(GL_TRIANGLE_FAN);
                    glVertex2d(ball[i].position.x, ball[i].position.y);
                    for(int u=0;u<=360;u+=5){
                        glVertex2d(ball[i].position.x+Math.cos(u)*Ball.RADIUS, ball[i].position.y+Math.sin(u)*Ball.RADIUS);
                    }
                    glEnd();
                }

            }
            System.out.println("Particle Amount: " + Ball.ballAmount);

            Display.update();
            Display.sync(60);
        }
        Display.destroy();
        System.exit(0);
    }

    public static void main(String[] args) {
        new Main();
    }
}

class Ball {

    public Vector2f position, velocity;
    public static final int RADIUS = 10;
    public static final int INITIAL_SPEED = 5;
    public static final int SPEED = 2;
    public static final int PARTICLES_PER_CLICK = 50;

    public static int ballAmount = 0;

    public double r, g, b;

    public static Random rnd = new Random();

    public Ball(double x, double y) {
        int angle = rnd.nextInt(360);
        position = new Vector2f((float) x, (float) y);
        velocity = new Vector2f((float) Math.cos(angle) * rnd.nextFloat() * INITIAL_SPEED, (float) Math.sin(angle) * rnd.nextFloat() * INITIAL_SPEED);

        this.r = rnd.nextDouble();
        this.g = rnd.nextDouble();
        this.b = rnd.nextDouble();
    }

    public void updatePosition(boolean gravity) {
        this.position.x += this.velocity.x * SPEED;
        this.position.y += this.velocity.y * SPEED;
        if (gravity) {
            double dx = this.position.x - Mouse.getX();
            double dy = this.position.y - (Main.HEIGHT - Mouse.getY());
            double distance = Math.sqrt(dx * dx + dy * dy);

            this.velocity.x -= (this.position.x - Mouse.getX()) / distance;
            this.velocity.y -= (this.position.y - (Main.HEIGHT - Mouse.getY())) / distance;
        } else {
            this.velocity.x *= 0.99;
            this.velocity.y *= 0.99;
        }

    }

    public static void createParticles() {
        for (int i = 1; i <= PARTICLES_PER_CLICK; i++) {

            ballAmount += 1;
            Main.ball[ballAmount] = new Ball(Mouse.getX(),Main.HEIGHT- Mouse.getY());
        }
    }

}

If the optimizer sees a final variable, it knows it's value will not change. What it does with that knowledge is up to it. It might do nothing (like it seems to happen in your case with PARTICLES_PER_CLICK or SPEED), or it might simply replace all occurrences of that variable with the actual value everywhere. There are no special rules, beyond do not change the values of final variables .

There are no special rules, beyond do not change the values of final variables.

Actually, Eclipse 4.23 (Q2 2022, seven years later) is now clearer:

Warning about changing final fields

Since Eclipse 3.1 Eclipse Java debugger allows changes on final field values.

While technically possible, the consequences of such changes are not trivial, could affect seemingly unrelated code and lead to various dangerous effects.

Therefore, with Eclipse 4.23 Java debugger shows now a new warning:

调试器警告——https://www.eclipse.org/eclipse/news/4.23/images/final_modification_warning.png

This warning is enabled by default and can be disabled via preferences:

更新首选项 - https://www.eclipse.org/eclipse/news/4.23/images/final_modification_option.png

Additionally, " org.eclipse.debug.ui.variableValueEditors " extension point is updated to allow custom products contribute their own " variableValueEditor " implementations to existing debug models and have even more control over final fields modifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM