Java Hello World to pass CheckStyle

So I am new to using checkstyle and my simple Java program HelloWorld, I am getting a lot of errors that I do not understand.

My code:

package <package_name>;

/**
* A simple class to compile.
*/
public class HelloWorld {

 /**
  * @param args standard main parameters
  */

    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

      

I am getting errors:

Line 6: Utility classes should not have a public or default constructor
Line 10: Parameter args should be final

      

Why is this happening? Do I need to create a private constructor for my class main

and make the default arguments final?

+3


source to share


3 answers


For Utility classes like your main class it is better to create a private constructor to prevent the java compiler from writing a no-argument constructor by itself Main()

.



Java always creates a copy of the parameters before sending them to methods. The last keyword here only means that variables cannot be reassigned within a method. (note that if you have a final object, for example in your case String[]

, you can still change the attributes of the object).

+4


source


1st issue answered here and documented here . The short answer is that you don't provide any instance members to the class HelloWorld

, so why should they instantiate? Therefore, it is recommended to create a constructor private

.



The second problem is indicated here . Short answer. Changing the value of parameters during the execution of a method algorithm can be confusing and should be avoided. Therefore it is recommended to declare themfinal

+2


source


You can create at least a private constructor by adding the following:

/**
 * Private default constructor
 */
private HelloWorld () {
}

      

This should go away. The reason is that the Java compiler "thinks" you want to make a utility class because it only sees public static methods. In this case, it is best to have your own default constructor.

For this example, "Hello World" is fine. But in RL code, you want to instantiate from your class HelloWorld

and then add a "business method" (the method that contains your logic) to it.

Edit: The warning on line 10 is good, as using final

for parameters generally means that you cannot change the value of the parameter:

public class Foo {
    public Foo () {
    }

    public void doBar (final Bar bar) {
        // Won't work:
        bar = null;

        // Will work!
        bar.callMe();
    }
}

public class Bar {
    public Bar {
    }

    public void callMe () {
        // Do something
    }
}

      

Now just remove this final

one and it will compile but remind you that you are changing the parameter. This causes unexpected side effects that (later in larger projects) the method changed some parameter "somewhere".

Just try to imagine: + 200k lines of code (no comments!) And no final

for parameters ... Complete chaos ...

The approved response poster said this, just details here and good reason.

+1


source







All Articles