Results 1 to 6 of 6
  1. #1
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Question The Java Tutorials - Passing Information to a Method or a Constructor

    I am just a bit confused about something in the tutorial on the page

    Passing Information to a Method or a Constructor

    They show a line of code that goes like this:

    public Polygon polygonFrom(Point[] corners) {
    // method body goes here
    }

    So from what I understand this is a constructor method for a Polygon object from the Polygon class.

    What I dont get is the name of the method polygonFrom()

    shouldnt a constructor for a Polygon just have the same name as the class?

    Because from earlier examples in the tutorial it seems to me that this is what has been done

    for example:

    public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
    }

    All help much appreciated.

    -XpoferenS

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: The Java Tutorials - Passing Information to a Method or a Constructor

    You are correct. That is not a constructor but a method. The second proof is the fact that it has a return type.

    You have to pay attention to what is said though.

    Passing Information to a Method or a Constructor
    See? Method OR constructor.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: The Java Tutorials - Passing Information to a Method or a Constructor

    Wow that was quick!

    OK so now my understanding is that you can just use a method to create an object, i.e you don't really need a constructor.

    Is that correct?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default Re: The Java Tutorials - Passing Information to a Method or a Constructor

    A constructor is not the same as a method. Methods and constructors have different purposes. A method is executed on an object and does something useful hopefully, optionally returning "something". That "something" could be an int, a String or another object.

    A constructor is a way of creating the object itself. So a class called Foobar could have various constructors, which are used to create it.

    Example:
    Java Code:
    public class Blah {
    
        public Foobar doSomethingUseful(String someParameter) { <-------------------------- this is a method
             Foobar f = new FooBar(someParameter); <-------------------- this creates a new object using the FooBar(String) constructor
             //even more useful things here
             return f; <--------- return the FooBar instance to the object that called this method "doSomethingUseful"
        }
    
    }
    
    public  class Foobar {
       public Foobar(String anythingHere) { <------------------ This is a constructor
          /// do something with anythingHere
       }
    }
    Edit: clarification/typos
    Last edited by SurfMan; 02-28-2014 at 02:48 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: The Java Tutorials - Passing Information to a Method or a Constructor

    Yes you do actually; that method will need to create an instance of Polygon and that will involve the invocation of a constructor that Polygon has :)

    Quote Originally Posted by SurfMan View Post
    A constructor is a way of creating the object itself. So a class called Foobar could have various constructors, which are used to create it.
    Nitpick (jebus I'm even more annoying this week than usual): the JVM does the constructing, the constructor's task is to initialize the created object instance. If I put it like that, I don't like the terminology anymore to be honest. Doesn't C# call it an initializer? That makes more sense to me.
    Last edited by gimbal2; 02-28-2014 at 02:48 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: The Java Tutorials - Passing Information to a Method or a Constructor

    Thanks a lot guys that was very helpful!

Similar Threads

  1. How do I pass information to a constructor?
    By Ormolu611 in forum New To Java
    Replies: 2
    Last Post: 02-24-2013, 06:33 PM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  3. Objects passing information to constructors.
    By Kirstielol in forum New To Java
    Replies: 6
    Last Post: 03-08-2012, 11:44 PM
  4. Replies: 2
    Last Post: 12-13-2011, 12:48 AM
  5. Replies: 6
    Last Post: 10-02-2011, 05:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •