-1

Let's assume we have a simple class:

public class Person {

    private String name;
    private int age;

    public Person()
    {
        this.age = 0;
        this.name = "Default Name";
    }

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

In an application which direcly(from another class) uses the second constructor, and indirecly(any other way than calling it from another class) uses the first one.

Is it OK to suppress unused warnings on the first constructor?

These are the two points of view:

  • Suppress the warnings - it hasn't got clearly visible usages and suppressing warnings doesn't produce any errors anyway..

  • Suppressing warnings is redundant and it shouldn't be done because it's unclear if a public method is used from a different point in an application

I am especially interested in info about empty constructors, which are used a lot in Enterprise Java.

Which is more correct? Thanks

Dropout
  • 13,653
  • 10
  • 56
  • 109

2 Answers2

4

I'd say it is nonsense to even raise an "unused" warning on any kind of public member of a class. I always configure my IDEs not to raise warnings in such situations.

In my opinion, it's useless to place @SuppressWarnings("unused") on any kind of public member, as such warnings shouldn't exist.

If it's public, this means that it's part of that class's contract, so any client of your class (or, extensively, of your API) may use it. So how can one detect if it's used or not in the entire Java world?

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
  • 2
    They do in intellij. The warnings are based on the build tooling and are not all part of the standard. – MadConan Nov 27 '13 at 13:30
  • 1
    The fields aren't public and public methods never raise an "unused" warning. I think the asker is getting the warnings because the private fields are never read anywhere in the code, i.e. they are probably useless. – fpw Nov 27 '13 at 13:31
  • @MadConan that's exactly why I'm asking. – Dropout Nov 27 '13 at 13:33
  • Thanks Andrei.. Is there any documentation you know of, which addresses this problem? Or possibly any other discussions which I could read, please? – Dropout Nov 27 '13 at 13:36
  • Honestly, I don't know of any documentation. I wrote the answer out of my experience and previous knowledge. But if you're talking about the public/private distinction and Java contracts, you can lookup some keywords like "encapsulation", "access modifiers" or "API". – Andrei Nicusan Nov 27 '13 at 13:43
1

No, it is not Ok to suppress the warning you get in the first contructor. It is better to fix the issue that it pin-points. What you want to do is rather:

public Person()
{
    name = "Default Name";
    age = 0;
}

and the warning "The allocated object is never used" is fixed.

Bludzee
  • 2,733
  • 5
  • 38
  • 46
  • I've edited it in, as was suggested in the comments. This however doesn't solve the problems for empty constructors. Different IDEs have different ways to look at it and both are a bit right. That's why I'm confused. – Dropout Nov 27 '13 at 13:34
  • I see you have modified your question in the meantime. You have fixed the warning in the first constructor now, so the question doesn't apply to the code sample anymore... – Bludzee Nov 27 '13 at 13:34