0

Reverse of general getter and setter statements.

What will happen if I reverse the getter and setter statements in the syntax below? Will the output remain same or change? If it changes then let me know how. :) public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { this.color = c; } }

21st Dec 2019, 5:06 PM
Santosh Pattnaik
Santosh Pattnaik - avatar
8 ответов
+ 2
Assume a car; Let car colour be red <- setter defines the colour What was the car's colour? <- getter tells the colour Setter - initialize/modify a member. It expects a new data for the member Getter - returns the member. It expects nothing, it gives instead Why would you want to reverse their functionality? what's the logic behind this thought? elaborate some more?
21st Dec 2019, 5:13 PM
Ipang
+ 2
What do you mean by reverse. Still they will be same. You can change method name but behavior will be remain same.
21st Dec 2019, 5:07 PM
A͢J
A͢J - avatar
+ 2
Santosh Pattnaik No output will be same.
21st Dec 2019, 5:27 PM
A͢J
A͢J - avatar
+ 2
Means it is not mandatory for the compiler to start compiling from the extreme ends of the code block, right?That is from the start of the code block or from the end of the code block.
21st Dec 2019, 5:29 PM
Santosh Pattnaik
Santosh Pattnaik - avatar
+ 2
Yes it's not mandatory. You can write Setter method above the Getter method or Getter method above the Setter method. Sequence doesn't matter.
21st Dec 2019, 5:34 PM
A͢J
A͢J - avatar
+ 1
Ipang Same question here. Why people want to change behavior if there is nothing to change. It's simple things if any Programming Language made some syntax and rule which we have to follow then why these things come in our minds.
21st Dec 2019, 5:17 PM
A͢J
A͢J - avatar
+ 1
Actually I thought there might be some other output for that. Thanku both
21st Dec 2019, 5:24 PM
Santosh Pattnaik
Santosh Pattnaik - avatar
+ 1
Oh I get it now 😁 You meant to swap their position in the source code. Well, in that case it's okay 👌
22nd Dec 2019, 5:38 AM
Ipang