Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Testing Constructor Overloading using Eclipse

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Testing Constructor Overloading using Eclipse

    I created a class/program in Eclipse with no errors. When I go to run it I select "run as" and I only get the option "run configurations" I don't get the option "Java Application".
    Last edited by Norm; August 31st, 2013 at 07:12 PM. Reason: Moved to IDE section


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    Does the class have a main() method? If yes, then post the code so that we can see what's wrong with it.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    In the Eclipse where u have written the class.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    package com.java.src;
     
     class Point3D {
    	double x;
    	double y;
    	double z;
     
    	Point3D(double ax) {
    		x = ax;
    		y = 1;
    		z = 1;
     
    	}
     
    	Point3D(double ax, double ay) {
    		x = ax;
    		y = ay;
    		z = 1;
     
    	}
     
    	Point3D(double ax, double ay, double az) {
     
     
    		x = ax;
    		y = ay;
    		z = az;
    	}
     
    	class Point3DOverloadConstructors {
     
     
     
    	public void main(String args[]) {
    		// TODO Auto-generated method stub
     
    		Point3D p1 = new Point3D(1.1);
    		System.out.println("p1.x = " + p1.x);
    		System.out.println("p1.y = " + p1.y);
    		System.out.println("p1.z = " + p1.z);
    		Point3D p2 = new Point3D(1.1, 3.4);
    		System.out.println("p2.x = " + p2.x);
    		System.out.println("p2.y = " + p2.y);
    		System.out.println("p2.z = " + p2.z);
    		Point3D p3 = new Point3D(1.1, 3.4, -2.8);
    		System.out.println("p3.x = " + p3.x);
    		System.out.println("p3.y = " + p3.y);
    		System.out.println("p3.z = " + p3.z);
     
     
    	}
     
     
    }
    }
    Last edited by jps; September 1st, 2013 at 02:36 PM. Reason: code tags

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    The main() method must be declared static, and it must also be in the top-level public class.

    Your class Point3DOverloadConstructors is currently enclosed in Point3D because (I think) you missed adding a close brace, '}', before the declaration of class Point3DOverloadConstructors. Fix those errors and make sure the open and close braces match up, and all should be well - at least with the Eclipse problem you've described. I didn't check to see that the program does as it should beyond that, because you didn't ask.

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    It was static but I get a error message that says "a static method can only be declared in a static or top level type"

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    Quote Originally Posted by michael.duffy31 View Post
    It was static but I get a error message that says "a static method can only be declared in a static or top level type"
    In the code you posted, it WAS not static, so you must mean that you added the static keyword.

    And, yes, I already explained to you that the class with the main() must be the top-level public class and that you had a close brace out of place or just plain didn't put one where it is supposed to be. This is the correct structure and formatting of the code you've posted. I also made the main() method static, but that's the only other code change I made, even though I want to fix your odd (mis)use of whitespace.
    package com.java.src;
     
    class Point3DOverloadConstructors {
     
     
     
        public static void main(String args[]) {
            // TODO Auto-generated method stub
     
            Point3D p1 = new Point3D(1.1);
            System.out.println("p1.x = " + p1.x);
            System.out.println("p1.y = " + p1.y);
            System.out.println("p1.z = " + p1.z);
            Point3D p2 = new Point3D(1.1, 3.4);
            System.out.println("p2.x = " + p2.x);
            System.out.println("p2.y = " + p2.y);
            System.out.println("p2.z = " + p2.z);
            Point3D p3 = new Point3D(1.1, 3.4, -2.8);
            System.out.println("p3.x = " + p3.x);
            System.out.println("p3.y = " + p3.y);
            System.out.println("p3.z = " + p3.z);
     
     
        }
     
     
    }
     
    class Point3D {
        double x;
        double y;
        double z;
     
        Point3D(double ax) {
            x = ax;
            y = 1;
            z = 1;
     
        }
     
        Point3D(double ax, double ay) {
            x = ax;
            y = ay;
            z = 1;
     
        }
     
        Point3D(double ax, double ay, double az) {
     
     
            x = ax;
            y = ay;
            z = az;
        }
     
    }

  8. #8
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    What do you mean by "odd (mis)use of whitespace"? Forgive me if this is a simplistic question, I am a old (30+ years) cobol programmer trying to learn a new language.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Testing Constructor Overloading using Eclipse

    It's more of a curiosity than anything else. The vertical spacing and resultant 'pacing' in your source code appears to be inconsistent and arbitrary. I see it often, and I always wonder why it's there.

    Since it is common in source code from new programmers, it's not odd in that sense. It is odd in the sense that 1) new programmers can never explain why it's there (they don't know it matters or that anyone else would even notice or care), 2) the existence of unnecessary and inconsistent whitespace doesn't 'bother' them, and 3) the inconsistent use of unnecessary whitespace is uneconomical and unstructured, contrary to the typical habits of a programmer.

    So, not a big deal, but a curiosity, and it pained me (a little) to re-post your code with the vertical spacing preserved.

Similar Threads

  1. constructor overloading
    By jamesjack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 13th, 2013, 12:12 AM
  2. Overloading a method
    By Lahoree in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2013, 11:46 AM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. Java automated testing tools for Unit testing
    By rameezraja in forum Member Introductions
    Replies: 2
    Last Post: April 14th, 2012, 08:51 AM