'Moving an enemy across the X-axis in Java

I am currently developing an 2d java game, a space shooter. I am adding a boss right now, and I want it to move to the right of the screen and when it reaches the edge to go all the way to the left. Once it reaches x=0, it should move to the right and so on. How should I do that? PS. I need to add this code to the tick() method. I tried this but it didn't work.

public void tick() {

    y += 1;

    if( y >= 20){
        y=20;
    }

    x += speed;

    if(x == 500)
    {
        x -= speed;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source