Results 1 to 6 of 6
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Why put other objects in an object's constructor...?

    When using
    Java Code:
    Scanner scan = new Scanner(system.in)
    , why do we put System.in in the constructor? What purpose does it serve and why can't we just not use it?



    Thanks!

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Why put other objects in an object's constructor...?

    A scanner needs an input to scan from. What should the scanner scan if you do not supply this?
    So of course: Objects can contain other objects. It is the same as in the real world. A car has a motor, wheels, seats, .... Why do you put all these ojects "in" a car? (Better: Why does a car contain all these items? "In" a car means normaly in the space inside the car and wheels are not inside. But a car contains them....)

    So it is clear: This containment is simply required.
    So you could do it in 2 ways:
    a) you directly hand over everything when creating an item.
    b) you first create an item and then insert items. Something like:
    Car car = new Car(); // Nothing here
    car.setMotor(someMotor);
    car.addSeat(driverSeat);

    The first way can be much easier to read. And it shortens the code. That is the reason why that version is used quite often.

    Konrad

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Why put other objects in an object's constructor...?

    Quote Originally Posted by MW130 View Post
    When using
    Java Code:
    Scanner scan = new Scanner(system.in)
    , why do we put System.in in the constructor? What purpose does it serve and why can't we just not use it?

    Thanks!
    Without the parameter, how would the Scanner (which, by its definition, is "A simple text scanner which can parse primitive types and strings using regular expressions.") now what it was supposed to be scanning?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Why put other objects in an object's constructor...?

    Quote Originally Posted by MW130 View Post
    When using
    Java Code:
    Scanner scan = new Scanner(system.in)
    , why do we put System.in in the constructor? What purpose does it serve and why can't we just not use it?



    Thanks!
    You use a Scanner wrapped around System.in so you can read stuff from the OS's shell/command prompt.

    Do you think you will be writing lots of command prompt/shell applications in your future? I don't think so. And so you won't be using System.in a lot either I can predict. Something to think about in the context of your question.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Why put other objects in an object's constructor...?

    Thanks to everyone for their replies. From my knowledge of constructors, they add characteristics to the object. I created a scanner object, and don't understand what it means to put System.in in the constructor.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why put other objects in an object's constructor...?

    If you read the API documentation for the Scanner class, you'd see that that constructor needs an InputStream to read from; System.in is an InputStream and a FileInputStream is also an InputStream and sockets can give you an InputStream; just create a Scanner with it and you can read from all those different InputStreams without changing one bit of code. That's what object oriented programming is all about ...

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Constructor/Object Help
    By MastaChief11 in forum New To Java
    Replies: 14
    Last Post: 08-13-2012, 12:14 PM
  2. Error Passing Object through a constructor
    By mpankhurst in forum New To Java
    Replies: 9
    Last Post: 12-15-2011, 11:12 AM
  3. Getting the calling object in a constructor?
    By Koala in forum New To Java
    Replies: 8
    Last Post: 10-31-2011, 04:27 AM
  4. Assigning array values to objects in a constructor
    By Dreaming in forum New To Java
    Replies: 8
    Last Post: 10-25-2011, 07:17 PM
  5. Constructor with unknown amount of objects?
    By Bernard Robitaille in forum New To Java
    Replies: 5
    Last Post: 03-01-2009, 06:00 AM

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
  •