Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

style question

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 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 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 …

861 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