Solved

style question

Posted on 2004-09-18
5
1,187 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error with Java/Cache JDBC Classpath 2 30
Java SE 8u111  Lot of stuff broke 11 54
replace a word with other 1 40
fibonacci ten numbers 4 26
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

911 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

17 Experts available now in Live!

Get 1:1 Help Now