June 24, 2013

Converting a stand alone java file to a file that's part of a project package?

Sam Peterson’s Question:

I still don’t fully understand where I’m required to use the “public static void main(String[] args)” header within the .java files of a project. Do you need to put that header in every .java file of a package?

I have been following along with chapter 3 in my book, dragging and dropping downloaded stand alone book source files into my project’s package, but some of the .java files in my package don’t like that “public static void main(String[] args)” statement, even though my opening and closing curly braces are in the correct place. Here’s an example of one of those files (the ERROR(S) are described in the code’s comments):

    public class Rectangle
{
   public static void main(String[] args){
   private double length;//ERROR: illegal start of expression
   private double width;

   /**
    * Constructor
    */

   public Rectangle(double len, double w)
   {
      length = len;
      width = w;
   }

   /**
    * The setLength method accepts an argument
    * that is stored in the length field. 
    */

   public void setLength(double len)
   {
      length = len;
   }

   /**
    * The setWidth method accepts an argument
    * that is stored in the width field.
    */

   public void setWidth(double w)
   {
      width = w;
   }

   /**
    * The set method accepts two arguments
    * that are stored in the length and width
    * fields.
    */

   public void set(double len, double w)
   {
      length = len;
      width = w;
   }

   /**
    * The getLength method returns the value
    * stored in the length field.
    */

   public double getLength()
   {
      return length;
   }

   /**
    * The getWidth method returns the value
    * stored in the width field.
    */

   public double getWidth()
   {
      return width;
   }

   /**
    * The getArea method returns the value of the
    * length field times the width field.
    */

   public double getArea()
   {
      return length * width;
   }

}//end of: public static void main(String[] args)

}//end of: public class Rectangle  ERROR: class, interface, or enum expected

The ERROR(S) came up after I added the “public static void main(String[] args)” to the existing Rectangle.java file. Any idea of why this occurs?

This occurs because You cannot have more than one main() method in a package. However overriding the main method is definitely allowed. What I mean is as, long the parameter number is differed you can override main() method.

public static void main(String[] args) {
   .....
}

And somewhere else in other or same class, you can add such class

public static void main(String arg1, String arg2) {
    .....
}

The ERROR: illegal start of expression is because you are using access modifier inside the method. Use all the access mofifier and variable declaration inside the class

public class Rectangle {

    private double length;//ERROR: illegal start of expression   
    private double width;

    public static void main(String[] args) {
    ....
    }
}

The ERROR: class, interface, or enum expected is because the class Rectangle is only housing a static method, your all the methods and parameters are inside the static method called main()

Here is your code which will compile without an error.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!