1

I have this:

public void log(Circle circOrig) {
    ...
}

And I'm trying to avoid doing this:

private void addPositions(PositionsLogger positionsLogger) {
    ...
    Circle circ = new Circle(0,0,0); //`circ` could be final
    circ.setPosition(0,0);
    posLogger.log(circ);
    ...
}

By doing this:

public static void main(String[] args) {
    ...
    posLogger.log(new (Circle(0, 0, 0).setPosition(0, 0)));
    ...
}

Which is obviously a compile error because log() requires a Circle, not a void.

How can I avoid having to declare a local variable for such a trivial purpose?

15
  • 7
    Make setPosition() return this. Commented Oct 2, 2013 at 14:24
  • 2
    Indeed, you won't be able to without some changes to the class. But honestly, what's the problem with declaring a variable? Commented Oct 2, 2013 at 14:27
  • 1
    Never do what @SotiriosDelimanolis said, returning this from a setPositions is not the proper way to develop. Commented Oct 2, 2013 at 14:27
  • 1
    @RamonBoza That's just not true. Heard of builders? stackoverflow.com/questions/5007355/… Commented Oct 2, 2013 at 14:28
  • 3
    @RamonBoza I point you to the Fluent interface (though personally I'm not a big fan). Commented Oct 2, 2013 at 14:28

4 Answers 4

6

There is no reason why not to have local variable. When you call new, It will create new object either way. Solution without local variable is more messy and less readable.

Variable is just pointer to memory, where "circle" is allocated. So when passing argument to your log function, you pass that pointer and inside log, you are working with created circle instance. There is no deep copy.

Sign up to request clarification or add additional context in comments.

2 Comments

-1 for lecturing instead of answering the question. I already know all that.
@deinocheirus If you know that, why are you using some "ugly" solutions ?
1

If you really want to not have a local variable, you can override the circle class like so:

posLogger.log(new (Circle(0, 0, 0){{setPosition(0, 0);}}));

4 Comments

I don't like this solution for two reasons. First, overriding the method you fix the position of the circle, which may need to be moved in other parts of the code. Second, you make two of the three Circle constructor parameters redundant leaving room for inconsistencies.
@mariosangiorgio how can you move a circle later without having a variable pointing to it? How do you know what the constructor parameters do? You haven't seen the code. They're not redundant.
Probably in this case you're right that the Circle it is not going to be moved. But how can we know that log, which has a reference to the object, does not move it? We don't have that method's code neither. Probably in this case the solution is going to provide the required behavior, but I am convinced that it is ugly and goes against best practice of programming. I assumed Circles constructor to require position and radius, as it happens in common implementation of circles.
@mariosangiorgio - minor point but that is not actually overriding the setPosition method. It is making a call to setPosition so you can happily call setPosition again from another part of the code (assuming you can get a reference to the circle of course)
1

Assuming you don't have access to the Circle code and don't like the overriding mechanisms (which look as ugly as having a local variable so are a bit pointless) then all you can do is define a helper method that creates the circle, sets its position and returns the circle.

I can understand why you want to do this but I think with Java being what it is, you're not going to get a brilliant solution without access to the Circle code.

Comments

0

As an alternative solution you should consider extend Circle adding a new constructor that does what you need.

You could either do that in the code of the class, if you can modify it, or in a new class.

public class ExtendedCircle extends Circle{
    public class ExtendedCircle(int x, int y, int x, int positionX, int positionY){
            super(x,y,z);
            setPosition(positionX, positionY);
    }
}

Note that I don't think that the creation of a new class is an ideal solution. It would be better to have an helper method or to keep the reference to the variable as reported in other answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.