Categories
discuss

Why is using a class as a struct bad practice in Java?

We recently had a code review . One of my classes was used so that I could return/pass more than one type of data from/to methods . The only methods that the class had were getters/setters . One of the team’s members ( whose opinion I respect ) said that having a class like that is bad practice ( and not very OOP ) . Why is that ?

Answer

There’s an argument that classes should either be “data structures” (i.e., focus on storing data with no functionality) or “functionality oriented” (i.e., focus on performing certain actions while storing minimal state). If you follow that argument (which makes sense but isn’t always easy to do) then there is nothing necessarily wrong with that.

In fact, one would argue that beans and entity beans are essentially that – data containers with getters and setters.

I have seen certain sources (e.g., the book “clean code”) arguing that one should avoid methods with multiple parameters and instead pass them as a single object with getters and setters. This is also closer to the “smalltalk model” of named parameters where order does not matter.

So I think that when used appropriately, your design makes sense.

Categories
discuss

Starting working with Eclipse RCP [closed]

I’m going to start maintaining an Eclipse RCP application for my current employer soon.
What are your experiences with this platform? What are the pros, what are the cons, what are the caveats that I should be aware of?
Also, what reading materials (books, tutorials) can you recommend? Most of the tutorials I found on the web are outdated, are there any good ones that are more up to date?
I started reading
Eclipse Rich Client Platform: Designing, Coding, and Packaging Java(TM) Applications
but since it’s also out of date, I got stuck pretty much in the beginning, and the book actually seems to be more like a collection of recipes to do something than a genuine book on the subject (it doesn’t explain the core concepts very well).
So, what can you tell me about the good sides and bad sides of developing an app with Eclipse RCP, and what are your reading recommendations?

Answer

Eclipse RCP isn’t as easy as people put it is. It’s essentially writing an Eclipse plug-in and you have to learn a whole new JFace/Eclipse way of doing things.

The Java Developer's Guide to Eclipse

Categories
discuss

Java Private Field Visibility

So I was making a class the other day and used Eclipse’s method to create the equals method when I realized that it generated the following working code:

class Test {
  private int privateInt;
  [...]
  public boolean equals(Object obj) {
    [...]
    Test t = (Test) obj;
    if ( t.privateInt == privateInt ) {
    [...]
  }
}

t.privateInt..???? It’s suppose to be private! So I guess there is one more field visibility other than private, protected, package protected and public.

So what is happening here? How is this called? Where would somebody use this? Doesn’t this break encapsulation? What if the class didn’t have a mutator and I changed this? Does this happen to C++ as well? Is this an OO idiom? If not, then why did Java do it?

Generally, where can I find information about this?

Thank you.

Answer

It’s accessible from different instances of the same class.

According to this page (bolding mine):

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class.

For clarity I’ll rewrite this line:

if ( t.privateInt == this.privateInt )

We can agree that “this.privateInt” should be allowed: you are accessing it from within the instance of class Test that the message “equals” has been sent to.

It’s less clear that “t.privateInt” should be visible, because t is a separate instance of class Test and we are not executing inside its equals method. However java allows this since both objects (t and this) are of the same class Test and can see each others private members.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..