Solved

style question

Posted on 2004-09-18
5
1,186 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
Comment Utility
Let me check that out

:)
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now