Solved

style question

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 44
spring jars download 1 27
Convert from a json string array to a Java object 3 32
Selenium docs api java index 3 12
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

809 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