• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with a statement

 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:...getter method will only show default value of field until setter is called in the foreign class

Each instance can have different value for the field through constructor. Then it can be made readonly by not providing a setter.
So it is not necessary that only default value will be shown through getter.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please if possible supply the link to that SO thread.

Go through the Java™ Tutorials about immutable objects. You should find an example there of a class whose fields are set up in the constructor and which are accessible only via getXXX methods.
You should never use the default values of fields. You should always initialise every field explicitly, even if it is to the same as the default value.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://googleweblight.com/?lite_url=http://stackoverflow.com/questions/6638964/set-and-get-methods-in-java&ei=SZn7BRS-&lc=en-IN&s=1&m=60&ts=1439027265&sig=APONPFl4yjsvGjj_IpO2-Ww9ZK3FXXcdYQ" target="_blank" rel="nofollow">here is the link
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sachin,
URL should be this.
http://stackoverflow.com/questions/6638964/set-and-get-methods-in-java

What you have posted is URL of googleweblight which is a feature of google to render a web page on a slow internet connection.
I guess the link provided by you will not be accesible on any other machine (atleast on my machine it is throwing 404).
Next time when you want to post some URL, if you have googleweblight URL in your browser, get back to the google search page and copy the link address from there and post.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am so sorry haven't noticed it before posting it.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we can set any value of variable in a class by constructor then what is the use of making setter private. I thought that statement meant that we can set any value of variable in a class by setter and then use the same value of variable in its subclass by getter method.

Is this is possible?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


So that no one can modify the value after instantiation.

This is not a very practical example, but think...If you have instantiated a money transaction class and someone else is coming and modifying the beneficiary account number. How dangerous it will be!!!
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Oh absolutely my pleasure. We are here to help each other, no?
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Foreign class even if have access to setter method it will be setting value for field for its instance. It won't be changing field of Base class
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If some one can please explain the significance of statement by providing a code to demonstrating it
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not mix inheritance concepts with getter/setter for now.
First let us resolve the simple problem.
Just forget about inheritance for 2 minutes and think how will you restrict modification of a particular field in an object? Ofcourse by not providing a setter for that field in the class.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means you are not taking the discussion to foreign class
You are talking about one class only then ..
Then making setter private won't help because it can be accessed from within the class
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think "foreign class" has anything to do with inheritance. I think it just means a class outside of your project, or a class you did not write.

Getters and Setters are for any class -- some would say even for the class itself. If the field is private, then not providing a setting makes the field read-only for any class besides itself. If you want to make a field read-only inside the class itself, declare it final and set it when you declare it or in the constructor.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If field is private, and setter is private then in the subclass's constructor you can use
super(value);
To initialize the field


By readonly you mean default value of variable as it is not initialized (in case of foreign class)
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote: . . .
By readonly you mean default value of variable as it is not initialized (in case of foreign class)

No, that is not what Knute meant at all.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what he meant by his statement
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will My statement be valid in case of subclass?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That statement isn't valid at all I am afraid.
Read-only means what is sounds like. You can read the value of a field from outside the class but you cannot write anything into that value.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:So what he meant by his statement


If you want to guarantee that the value of a variable won't change, you can make it final and static.

Now you know that the value of PI won't change while the program is running.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

… because inside the class the variable is not read‑only.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


If the superclass has

...then it doesn't matter if there is a setter or not and it doesn't matter if a class inherits it or not. But what we're talking about is:

In that case you can only set foo inside MyClass and nowhere else, inherited or not.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for aadding screen shot in the place of code.
Its very difficult to write code on a mobile screen
I tried writing it but I failed so I added screenshot
Screenshot-(21).png
[Thumbnail for Screenshot-(21).png]
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have any questions or comments about the code? It seems like you have the basic idea of a private setter making a field readonly.

Here's the code in text, FWIW:

 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks every one
I just wanted to check if I understood concept properly by showing my code

Are there more ways by which foreign class can only read the value but not set it
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not really a completely different way, but instead of instantiating Alpha in Alpha itself and passing it as an argument in Beta's constructor, you can instantiate Alpha in Beta with a parameterized constructor. Why I am saying this because normally in web applications we seldom instantiating a class within the class itself. Because there are no main() methods in a web application. The only main() method belongs to one of the classes of the application server which is called during startup.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't mentioned how will you use paramaterised constructor
So considering all the aspects you can use the constructor in 2 ways
1-if you are passing value of variable (of x)
Then you won't be using set method to set fields

2-if you are passing reference of alpha then you'll have to create 2 object of Alpha and pass the reference of one object into the constructor of other.
Then you can set fields by set method
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me show you what I meant.


 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I understood this but in this case you are not using setter method you are setting the field by constructor
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+one more thing you are making alpha a local variable so you will have to invoke get method within method block
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:Thanks I understood this but in this case you are not using setter method you are setting the field by constructor


Yes that is exactly what I said in my previous post.

Tapas Chand wrote:...you can instantiate Alpha in Beta with a parameterized constructor.

 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:+one more thing you are making alpha a local variable so you will have to invoke get method within method block


You are right. That is what methods are used for - to write business logic and call other methods.
Even if I make alpha a class level variable, then also I have to invoke getX() method within the method block.
By the way, where do you expect to invoke getX() method if not invoking within method block?
 
Ranch Hand
Posts: 62
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



First of all, a field F of a class can be set in the constructor, in any method other than a setter that simply computes a value and sets it to that field, etc.
Thus, when the setter is private, and all other public methods do not change that field F, a foreign class can only read field F and not modify it.

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), and it is something good. I do not see anything bad about. You simply create an object, call its methods (that may or may not change field F) then you read field F with its getter method. Nothing wrong with that, as it does not make the getF() method useless.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)


This is half true. Have a look at my previous example.
Setter is never called, any other method is not changing the value of x, still getX() will return "8" and not the default value of x (thanks to parameterized constructor).

 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Whatever I demonstrated is for sake of example only.
But in reply to your statement, this is correct for a single threaded environment. You declare as instance variable and do a lazy initialization in method. Nothing to worry about.
But in a multi threaded environment things becomes complex. You need to worry about instance variables accessibility to multiple threads and choose the location of declaration carefully.

By this, I am not at all advocating method level declaration, but location of declaration needs to be taken care of in a multi threaded environment.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
. . .

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.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup by passing value by constructor or setter .You can check the value before assigning it.But I think Lucian just wanted to say if we don't pass value through constructor or setting field by any method then getter will bring default value
 
reply
    Bookmark Topic Watch Topic
  • New Topic