Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Each instance can have different value for the field through constructor. Then it can be made readonly by not providing a setter.Sachin Tripathi wrote:...getter method will only show default value of field until setter is called in the foreign class
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:If we can set any value of variable in a class by constructor then what is the use of making setter private
Campbell Ritchie wrote:Thank you Tapas Chand for sorting out the URL.
This is a good discussion, apart from people being rude about how simple the question is.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
All things are lawful, but not all things are profitable.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
No, that is not what Knute meant at all.Sachin Tripathi wrote: . . .
By readonly you mean default value of variable as it is not initialized (in case of foreign class)
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:So what he meant by his statement
All things are lawful, but not all things are profitable.
… because inside the class the variable is not read‑only.Bear Bibeault wrote:Bear in mind that the class itself can be changing the value willy-nilly. Changing a variable via a setter isn't the only way it can be changed.
Sachin Tripathi wrote:If field is private, and setter is private then in the subclass's constructor you can use
super(value);
To initialize the field
All things are lawful, but not all things are profitable.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
All things are lawful, but not all things are profitable.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:Thanks I understood this but in this case you are not using setter method you are setting the field by constructor
Tapas Chand wrote:...you can instantiate Alpha in Beta with a parameterized constructor.
Sachin Tripathi wrote:+one more thing you are making alpha a local variable so you will have to invoke get method within method block
Sachin Tripathi wrote: I saw this statement written in an answer of one question on "stackoverflow
a very common case: If you only want to allow read access to the field for any foreign classes - you can do that by only providing a public getter method and keeping the setter private or not providing a setter at all.
I am unable to understand it as getter method will only show default value of field until setter is called in the foreign class
What is the use of getter method in foreign class?
Lucian Whiteman wrote:So far you have understood things correctly, your statement is a bit misleading though: "getter method will only show default value of field until setter is called in the foreign class"
This is indeed true(with the addition that also other methods other than a setter may change that field F)
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:Yeah you are right
But one have advantage by declaring it as a class level that you will be having the reference for indeterminate time
You may not see anything bad about that, but I do. What if the default value (0/0.0/false/null) of the field is one you don't want? You now have the object in an inconsistent state, What if you call getXXX before setXXX? You will end up returning a value you don't want; the most dangerous possibility there is null because you are now letting nulls loose into your program. Look what the top mistake is here. That is why Tapas Chand's suggestion of requiring the value as a constructor parameter is so much better. If you do not supply a value, the compiler will not let you create an instance. So you must supply a value. You can also use both constructors and setXXX methods to validate the value.Lucian Whiteman wrote: . . ."getter method will only show default value of field until setter is called in the foreign class"
. . . I do not see anything bad about.
. . .
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.