0

My question is very simple and novice. I have a class, and want to create instance of it. So should I use temporary variables and then assign the values using setters or should I create an object and then directly assign user input to setter.

class A {
int a;

A(){
    a=0;
}

A(int a){
    this.a=a;
}

}

Now how should I create objects?

  1. Should I use temporary variables and then use constructor to create object.
  2. Should I create object and directly assign user input to instance variable. for example
    aObj.setA(getUserInput());

Please advice.

1
  • 1
    One way isn't necessarily any better than another. If the object provides a constructor that accepts an argument, generally you should use that since the constructor may do more with the passed in object than simply set an instance variable. Commented Aug 3, 2014 at 13:50

4 Answers 4

2

To the first question: Either. Depends on a lot of things like:

  • Do you already know what the initial value or default value should be?
  • Would the class work properly/improperly without it set to anything at first?
  • Should the internal variable even have a setter at all?

Example: if I had

class bonkinator {
int timesBonked;

bonk(){
  timesBonked++;
}

getBonks(){
  return timesBonked; // thanks for the correction, Unihedron
}

...

Then chances are I wouldn't want other code to meddle with timesBonked, so I wouldn't give it a setter. So I'd want to set timesBonked to some initial value in the constructor, and whether or not that value is something I choose or something the calling code chooses depends on what we're trying to do.

To the second question: depends again.

  • Do you plan on re-using that thing from getUserInput()?
  • Would calling getUserInput() either annoy the user or re-run slow code (e.g. try to read something from a database that hasn't changed) for no added benefit?
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! i got what i wanted.
1

Invoke new A(); or new A(42); in the same package.

Please see the following:

A a = new A(getUserInput());

A a = new A();
a.a = getUserInput();

There is not much difference but choose your preference in terms of code style: The Java compiler will optimize most of it.

Comments

1

That totally depends on the design but generally 2nd way is preferred in production code because you might add new variables in the class later and then if you change the constructor you might break existing code.

Also

A(){
    a=0;
}

is not necessary as instance variables get initialized with default values i.e 0 for int.

Comments

1

There is no specific way to do that. It is purely based on what you wanna do. You can either have a setter to set the value after initializing the instance. Or you can have a parameter constructor and pass the value at the initializing point. Or even you can have fixed value if you want at the initializing. It is entirely up to you to decide what is the best approach for you to what you are trying to do.

1 Comment

@Unihedron I mean id the initializing value is always some fixed value then it is not a problem to have a such a way. You can change it using setter later.

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.