Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: constructors

  1. #1
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default constructors

    if you had something like this
    Code:
    class Blah
    {
    
       void Blah()
       {
       }
      
       Blah()
       {
       }
    }
    I know Blah() is a constructor, but the class Blah is not right?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The second Blah() is a constructor, yes.

    Of course the class isn't a constructor. A constructor is a method that sets up an instance of its class to be ready for use.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    SO it has 2 constructors right?
    I read somethign about what the void meant and didnt really understand it, can you try and explain it to me?
    Also what does encapsulated mean?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    SO it has 2 constructors right?
    No, only one constructor. The other method has a return type, so it's just a standard instance method.
    I read somethign about what the void meant and didnt really understand it, can you try and explain it to me?
    It's the return type declared when a method doesn't return a value.
    Also what does encapsulated mean?
    encapsulate
    v 1: enclose in a capsule or other small container
    I can't be any more specific without context.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Would this be tightly encapsulated?

    so void Blah() is not a constructor since it does not return a value?

    if im not mistaken an instance variable would be like String or int right?
    Last edited by craigtb; 01-30-2007 at 06:45 PM.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Would this be tightly encapsulated?
    "Tight encapsulation" refers to making the innards of the class as inaccessible to outside objects as possible. The idea is that this promotes the design of classes with consistent and powerful interfaces (I don't use the term in its Java sense) whose use requires no knowledge of the workings of the class.
    so void Blah() is not a constructor since it does not return a value?
    No, it's not a constructor because it has a defined return type. The fact that that return type is void is neither here nor there.
    if im not mistaken an instance variable would be like String or int right?
    An instance variable (known as a property in Java parlance) is any property that belongs to instances of the class, as opposed to, say, a local variable in a function or a static property (which belongs to the class itself).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    so like
    Code:
    public Blah
    {
      private double height, distance;
      void Blah()
      {}
      Blah()
      {}
    }
    that would have 2 instance variables?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, height and distance.
    Code:
    public Blah
    however, is a syntax error. Since it's a class, you cannot omit the class keyword:
    Code:
    public class Blah {
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK thanks!

  10. #10
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    If i was to write a constructor for a class PersonInfo would it be somehting like this?
    Code:
    class PersonInfo
    {
      private String name, address;
      private int income;
      PersonInfo()
      {
         return name;       \
         return address;      im not sure about this way of writing it
         return income;     /
      }
    }
    Last edited by craigtb; 01-30-2007 at 09:51 PM.

Bookmarks

Posting Permissions

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