Default Constructor

default constructor that initializes instance fields

Page 1 of 1

7 Replies - 583 Views - Last Post: 07 October 2009 - 09:46 AM Rate Topic: -----

#1 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Default Constructor

Post icon  Posted 07 October 2009 - 07:49 AM

Hello experts,

I created a default constructor with the following instance fields
make: GM
model: Pontiac
cost: 20000

I just wanna know if I did it right? :)
class Automobile
{
	public Automobile()
	{
		make = GM;
		model = Pontiac;
		cost = 20000;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Default Constructor

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,453
  • Joined: 24-September 08

Re: Default Constructor

Posted 07 October 2009 - 07:53 AM

Well, I don't see any variable declarations or types so I'm assuming that if the types match then sure.
Was This Post Helpful? 0
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Default Constructor

Posted 07 October 2009 - 07:56 AM

View PostNeoTifa, on 7 Oct, 2009 - 08:53 AM, said:

Well, I don't see any variable declarations or types so I'm assuming that if the types match then sure.


What you mean by "types"?
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Default Constructor

Posted 07 October 2009 - 08:09 AM

She means that you don't have any variables in your class. Right now, as your class is, it will generate compiler errors saying it cannot find variables make, model, cost, GM, Pontiac.

(GM and Pontiac should probably both be in quotes, since they're Strings)

You need something like this...

class Automobile
{
    // these are the variables of the class.  Each object will
    // have a unique set of these.
    String make, model;
    double cost;

    public Automobile()
    {
        make = "GM";
        model = "Pontiac";
        cost = 20000;
    }
}

Was This Post Helpful? 0
  • +
  • -

#5 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Default Constructor

Posted 07 October 2009 - 08:29 AM

View PostLocke, on 7 Oct, 2009 - 09:09 AM, said:

She means that you don't have any variables in your class. Right now, as your class is, it will generate compiler errors saying it cannot find variables make, model, cost, GM, Pontiac.

(GM and Pontiac should probably both be in quotes, since they're Strings)

You need something like this...

class Automobile
{
    // these are the variables of the class.  Each object will
    // have a unique set of these.
    String make, model;
    double cost;

    public Automobile()
    {
        make = "GM";
        model = "Pontiac";
        cost = 20000;
    }
}

It's not meant to be compiled. It's just a project that shows that I'm familiar with the default constructor that initializes instance fields.
I appreciate the explanation.
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Default Constructor

Posted 07 October 2009 - 08:34 AM

Oh, well then yeah. Looks good to me as long as you have the right types/variables. :)
Was This Post Helpful? 1
  • +
  • -

#7 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Default Constructor

Posted 07 October 2009 - 08:53 AM

View PostNeoTifa, on 7 Oct, 2009 - 08:53 AM, said:

Well, I don't see any variable declarations or types so I'm assuming that if the types match then sure.


Thank you :)
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,453
  • Joined: 24-September 08

Re: Default Constructor

Posted 07 October 2009 - 09:46 AM

Np. Glad you understand the concepts ^__^ You should click the "This Post Was Helpful" button under my post then.
Was This Post Helpful? 1

Page 1 of 1