?
Solved

style question

Posted on 2004-09-18
5
Medium Priority
?
1,195 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:bnblazer
5 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 12091863
Let me check that out

:)
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 1000 total points
ID: 12092071
I think what they are talking about is that classes that just have static methods probably never will get instanciated.  If that is the case, then you can enforce that rule by creating a private constructor.  The java.lang.Math class is like this.  It is a utility class that just contains static members and static methods.  If you try to instanciate it like this:

Math myMath =new Math();

you will get a compiler error.  The math class is declaired something like this:

public class Math {
  private Math() {}  //This class cannot me instanciated.

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

}
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12092075
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.
0
 
LVL 7

Expert Comment

by:lhankins
ID: 12092218
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.
0
 
LVL 1

Author Comment

by:bnblazer
ID: 12092437
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question