Results 1 to 4 of 4
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default What's a constructor?

    I've studied it, but there's too much lingo in Java, so I have no clue what it is. The Java Docs didn't necessarily help.

    Also: When you make a new Exception and Throwable thingy ( I don't know), why do you need it, if you already know what's going to happen? Like for instance why should I make a method or what ever that catches an error that I made when I know what that method is going to catch? Are you going to create an error for that method to catch or purpose? I just don't see the functionality of making a method or whatever that catches an error, because if you made something that catches it, you shouldn't be making those types of errors in the first place...
    Last edited by Army; 04-18-2012 at 02:57 PM.

  2. #2
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: What's a constructor?

    A constructor creates an instance of a class, the best way to explain this is you have a blueprint for an apple.

    so you call the constructor "apple" and pass it the variable's it needs to construct an instance of the class.

    say our vairable's are coming from a form and the user chooses the options below then hits enter

    Colour = red
    Size = medium
    Price = 10

    our button would call apple.create(string colour, string size, string price)

    and pass the variable across to the Constructor and the Constructor would produce a "Medium sized red apple"

    this save making all the different types of apples, you just make the blueprint as it where and change the variables to suit the users needs hopefuly this helps.

    On your second question what if you overlook a user input and they can enter bad chars into your nice function? and it falls over?

    Well the catch statment simply catches it and you can add mutiple different throw statments so you only have to add the one catch statment to catch all of your possible throw errors and give you a detailed report on what went wrong.
    Last edited by Beattie282; 04-18-2012 at 03:15 PM. Reason: added stuff

  3. #3
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: What's a constructor?

    A constructor is what Java uses to create an instance of a class, you can think of it as a cookie cutter. You can make many cookies (instances of a class) using the cookie cutter (class' constructor). Each call to a class' constructor returns a new instance, separate and distinct from all other instances of that class.

    You might want to start by reading more about them here.

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: What's a constructor?

    As for exceptions, they are the unexpected, not the expected. But you might know some information that allows you to group these exceptions into different kinds of exceptions.

    In your catch clause, you are specifying what specific exceptions you want to handle. If you don't want or need to handle them, you can swallow them (do nothing in the catch) or percolate them up to the caller (throw the exception). If you do want to handle it, you may decide you need to add information to the exception for diagnostic purposes and then throw it so it can percolate up to the caller to handle.

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  2. No-arg constructor??
    By collwill in forum New To Java
    Replies: 6
    Last Post: 03-09-2011, 02:50 AM
  3. What does this Constructor Do ?
    By Ciwan in forum New To Java
    Replies: 1
    Last Post: 10-02-2010, 06:24 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 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
  •