Link to home
Start Free TrialLog in
Avatar of bnblazer
bnblazer

asked on

style question

I am currently learning java.  So far so good.  One of the tools I am using to help with this is the 'checkstyle' plugin for Eclipse.  I is giving me a style warning for the following code:

final class Buzz {

    /**
     * @param args none
     */
    public static void main(String[] args) {

//Set the variables
        int x = 1, y = 100;

//Run through the numbers and print the output
        for (int i=x;i<=y;i++) {
            if (i % 7 == 0) {
                System.out.println("Buzz");
            } else {
                  System.out.println(i);
            }
        }
    }
}

The warning is at 'final class Buzz { '.  It says "Hide utility class constructor -Utility classes should not have a public or default constructor."

I have tried to look this up, but the only thing I can find is about java.util.  Nothing about the style warning.  I am not sure if this warning is relevant or not, and if it is, how would I hide the constructor? I am unsure what it is talking about.

Obviously this code did come from a class assignment, but it has been turned in and graded (20/20).  I brought this question to the instructor, and he wasn't sure about it either.

Any help is appreciated.

Thanks,
Brian
Avatar of suprapto45
suprapto45
Flag of Singapore image

Let me check that out

:)
ASKER CERTIFIED SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry the Math should be:
public class Math {
  private Math() {}  //This class cannot me instanciated.

  public static double PI=3.141592653...;
...More static constants
  public static int max(int a, int b) { return (a>b) ? a : b;}
...more static methods

}

EVERY method and variable is static.
I think Mogal is right...  I bet its just looking at your class and going "Ok - I see this has only all static methods, why would you ever want to create an instance of this class...?"    To get it to go away, try declaring a private or protected constructor.
Avatar of bnblazer
bnblazer

ASKER

after more research and reading the answers here, I tried the suggestions.  No difference.  I think that I may be dealing with a bug in checkstyle.  Look at my for statement.  I keep getting a warning for that too, saying that whitespace is not allowed around <=, or ++.  But as you can see, there is none.