• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declarations  RSS feed

 
Appu praveen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one explain what is happening in below mentioned java code.

Why in method insert, int l, int w is declaring & it is assigning to length & width.
---------------------------------------------------------------------------------

---------------------------------------------------------------------------------
The complete program is show below.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically the insert() method is used to set the state of the Rectangle instance. Typically in cases like this a constructor would be used for this purpose:


The main() method would then look something like:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Please indent the code properly and don't try to squeeze too much onto one line: your area method is very difficult to read. I shall try posting it again so you can see what it should look like.

Your lines 2-3 declare the two fields length and width. You initialise them in the insert method (line 6-7). When you write int l, int w on line 5, you are declaring parameters called l and w. Whichever values are passed as arguments to those parameters are used later on.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags <-- that's a link. I added them this time. See how much easier it is to read?

What you are seeing is a (poor) example of the principle of using setters. Google for "encapsulation" and "information hiding". But as I said, that's a poorly written example. Is that from a book?

I would have written like this:


Does that make it clearer or did I just confuse you even more?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how they should have indented that methodI agree with Jelle. A method should do one thing and one thing only. Your area method does two things, calculating the area and displaying it. You can have a display method or override toString.The \u00d7 character looks like this: ×
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
What you are seeing is a (poor) example of the principle of using setters. Google for "encapsulation" and "information hiding". But as I said, that's a poorly written example. Is that from a book?

I would have written like this:

I don't know if I necessarily agree that accessing private members from the same class needs to go through getters/setters. In fact, one of the criticisms of Java is that there is a tendency to write verbose code this way when a more direct approach would be just as good and more concise. Languages like Groovy actually do away with the need to write explicit getters and setters. I think Scala might have something like that as well.

Another thing to consider is inheritance. If you access private fields through getters and setters exclusively, that could open you up to getting some surprises later on when an overriding version does something other than set/get the field value. At the very least, I would mark the getters and setters in a non-final class as final so that any attempt to override them in a subclass would trigger a compiler error and, hopefully, a discussion about the class design assuming, of course, that everyone on the team that's working on the code has that kind of discipline. This brings us back to the fact that inheritance can break encapsulation of objects if you're not careful. See also getters and setters break encapsulation. Not trying to argue against them in general but you should at least be aware of the pitfalls involved.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
I don't know if I necessarily agree that accessing private members from the same class needs to go through getters/setters.

Interesting point and good links. I found this article especially interesting. It seems I have some habits to break.

We're wandering off topic from the OP's original question, so I'll stop here, but thanks for that information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!