0

I'm new to Java and am working on a homework problem. The problem specifies to create a Temperature class with a Constructor that accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field. The setFahrenheit method accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field.

This seems redundant to me. Wouldn't the setFahrenheit method do exactly what the Constructor does?

JacobA
  • 25
  • 4

7 Answers7

1

Nope. What happens if the temperature changes? You can't create a new Temperature class because that would mean you're creating a completely new object. setFarenheit() allows you to change the temperature in that specific class.

Here's another better example: Say you have a class called Person and you pass the person's age in the constructor. You should have a setAge() method in the class because the Person is eventually going to get older and it would be rather inefficient if you create a copy of the person just to change his/her age.

ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • Thanks for the reply. Would the method setAge() need an argument if the Constructor has parameters? – JacobA Sep 05 '15 at 19:15
  • It wouldn't NEED an argument. You could just increment the age by 1 if you want or you could give it an argument and set the class attribute to that. – ninesalt Sep 05 '15 at 19:18
1

1)Suppose if you have also one empty constructor and you are creating one Temperature object temp as

temp=new Temparature();

For temp, ftemp field is still empty. So , if you want to set ftemp, you will use setFahrenheit method as

temp.setFahrenheit(12);

2)Again, if you are creating Temparature object with ftemp in the constructor,

temp=new Temparature(22); 

Next, what you will do if you want to change the ftemp for this temp object ? you will use setFahrenheit() method with new ftemp value again, right ?

Hope this clears your doubt.

N Kaushik
  • 2,198
  • 18
  • 29
0

Quoting the answer given at the following URL: Methods vs Constructors in Java

The important difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist. Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new. The definitions of constructors and methods look similar in code. They can take parameters, they can have modifiers (e.g. public), and they have method bodies in braces. Constructors must be named with the same name as the class name. They can't return anything, even void (the object itself is the implicit return). Methods must be declared to return something, although it can be void.

Courtesy of rgettman

Community
  • 1
  • 1
abhi
  • 1,760
  • 1
  • 24
  • 40
0

When you construct a new Temperature object, you give it a Fahrenheit value. However, what happens if you want to change the value of the object? You can't use the constructor again, as this would make a new object instead of changing the current one. So, you can call the setFahrenheit method on your object instead, which will change its Fahrenheit value.

helencrump
  • 1,351
  • 1
  • 18
  • 27
0

Yes and no. The constructor is used to instantiate an object of Temperature, and then sets the temperature to the value you pass into it. The method would allow you to change the value of the temperature anywhere along your code.

Turtle
  • 1,369
  • 1
  • 17
  • 32
0

The constructor is only called once. After the object is created you can't "recreate" it, you can only make a new one. If you want to change an existing one you need to use the setter.

Zarwan
  • 5,537
  • 4
  • 30
  • 48
0

Yes, but there's more to this than what may appear on the surface.

  • If that is the only constructor in the class, then adding that setter makes the class mutable; that is, even though you must specify the value at instantiation, you can change it later without a new instance.

    This is a common pattern if one wishes to create a mutable object. Sometimes, it's just simpler to allow this than to force an immutable pattern (although it's bad practice if one wishes to use this in a multi-threaded context, since you can't guarantee its thread security very easily).

  • If that is not the only constructor; that is, you have a no-arg constructor which does no initialization, then the variable will be set to its default value. In this case, you would have to instantiate the object, and then set the ftemp field to something you cared about.

    This is typical in POJOs, where you can instantiate an object in many ways. Depending on how you interface with the object, this approach may be more desirable.

Makoto
  • 104,088
  • 27
  • 192
  • 230