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; } }
8 Answers
+ 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?
+ 2
What do you mean by reverse. Still they will be same. You can change method name but behavior will be remain same.
+ 2
Santosh Pattnaik No output will be same.
+ 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.
+ 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.
+ 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.
+ 1
Actually I thought there might be some other output for that. Thanku both
+ 1
Oh I get it now đ
You meant to swap their position in the source code. Well, in that case it's okay đ