Results 1 to 6 of 6
  1. #1
    RattleSnake is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default What's a constructer?

    Hi guys,

    I tried Googling it and all I got was complicated explanations so I came here for help.. I just started Java 2 days ago, so I don't know too many 'terms'.

    From what I've gathered.. Is this a constructer? :S

    Java Code:
    Test random = new Test();
    Or.. Am I mistaken?

  2. #2
    nostromo4 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    Yes, assuming there is a class Test with a standard constructor.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Not quite, that line uses a constructor tocreate an instance of type Test.

    The constructor is the Test() part. A constructor combined with new creates an instance of some object. Creating objects is important in oop programming. A lot of different constructors can be made, they can take arguments of multiple different types.

    If you don't supply a constructor the compiler will supply a default constructor for you to use. It takes no arguments and sets all numbers to 0 or 0.0, and all objects to null.

    If you need more clarification, please ask questions.

  4. #4
    RattleSnake is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Okay so what about this.

    Java Code:
    import.java.awt.Point
    
    class TestingContrsucters {
    
    void main(String[] arguments) {
    Point testing = new Point(4, 2);
    
    }
    
    
    }
    So the constructer here is (4,2);?

    If so, that's 2 constructers.. Or?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The constructor there is Point(2, 4)

    It's a constructor which takes two arguments and uses the arguments to initialize the instance variables in the point class to 2 and 4. Here is a quick example

    Java Code:
    class Point{
      int x;
      int y;
      public Point(int x, int y){ //notice that there is no return type
        this.x = x;
        this.y = y;
      }
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

Posting Permissions

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