+ Reply to Thread
Results 1 to 8 of 8

Thread: Java Question

  1. #1
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Java Question

    Another one of my famous quick questions (Java noob):

    I have an error:
    http://i33.tinypic.com/o7368g.png
    and a source:
    http://pastebin.com/d732cdaba
    when compiled produces:
    http://pastebin.com/m7ea0a6ba

    Whats wrong?

    Problem semi-solved:

    Netbeans didn't recognize the int constructor, what is the difference between these two constructors, or is Netbeans just having a fit?:
    Code:
    Circle(int radius) {
            this.radius = radius;
        }
    Code:
    public void Circle(int radius) {
            this.radius = radius;
        }
    Last edited by Twinkie; 10-31-2009 at 09:18 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Java Question

    Quote Originally Posted by Twinkie View Post
    Code:
    public void Circle(int radius) {
            this.radius = radius;
        }
    This isn't a constructor, it's a method named Circle that takes an int and doesn't return anything.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Java Question

    Quote Originally Posted by Twinkie View Post
    Another one of my famous quick questions (Java noob):

    I have an error:
    http://i33.tinypic.com/o7368g.png
    and a source:
    http://pastebin.com/d732cdaba
    when compiled produces:
    http://pastebin.com/m7ea0a6ba

    Whats wrong?

    Problem semi-solved:

    Netbeans didn't recognize the int constructor, what is the difference between these two constructors, or is Netbeans just having a fit?:
    Code:
    Circle(int radius) {
            this.radius = radius;
        }
    Code:
    public void Circle(int radius) {
            this.radius = radius;
        }

    Constructors shouldn't have return type. Just remove the void in front of Circle(int radius) method.

    and run it.
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  4. #4
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Java Question

    Quote Originally Posted by misson View Post
    This isn't a constructor, it's a method named Circle that takes an int and doesn't return anything.
    In theory, but the classname is "Circle" too, so the method's name is illegal. Besides that, method names should never start with a capital.

    Constructors are usually defined as
    Code:
    public <classname>(<arguments) {
        <body>
    }
    And as gsonline said, that's equivalent to removing the "void" part of the method signature.

  5. #5
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Java Question

    Well, it is more of a logical question. I already know how to fix the problem, I just want to know why. If a constructor doesn't return anything, then why is putting a void return type wrong (like how specifying that a method is public and specifying engender the same result)?

  6. #6
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Java Question

    The constructor is for initializing the values while creating the object of the class. The java compiler distinguishes the constructor by its name same as classname and no retun type. There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. If it has return type then compiler takes it as method rather than constructor

    Hope this helps...
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  7. #7
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Java Question

    Oooo, I confuse no return type with no return value. Thanks for your help

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Java Question

    Quote Originally Posted by marshian View Post
    In theory, but the classname is "Circle" too, so the method's name is illegal. Besides that, method names should never start with a capital.
    Legal, but discouraged. From section 8.4 "Method Declarations" of the Java Language Specification, Third Edition:
    A class can declare a method with the same name as the class or a field, member class or member interface of the class, but this is discouraged as a matter of syle.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Is Rand Paul a Freemason?
    By allofus in forum Crossfire
    Replies: 6
    Last Post: 10-02-2009, 04:52 AM
  2. Java Problems
    By runeps in forum Programming Help
    Replies: 5
    Last Post: 08-13-2009, 01:43 AM
  3. VPS - JAVA JDK Help
    By allofus in forum Programming Help
    Replies: 12
    Last Post: 03-14-2009, 12:09 PM
  4. Anfy Java Applet Online problems
    By Tharu in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 08-02-2008, 09:05 PM
  5. Odd question but will I be banned for this?
    By sielk in forum Free Hosting
    Replies: 3
    Last Post: 09-28-2007, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers