7 Replies - 23930 Views - Last Post: 23 February 2009 - 08:10 AM Rate Topic: -----

#1 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

No Argument Constructor

Posted 23 February 2009 - 12:21 AM

/*
	File: Bicycle.java
*/

public class Bicycle {

	// Instance field
	public String ownerName;
	public int licenseNumber;
	
	// Constructor
	public Bicycle( String name, int license ) {
		ownerName = name;
	  licenseNumber = license;
	}
	
   // Returns the name of this bicycle's owner
	public String getOwnerName( ) {
		
		return ownerName;
	}

	// Assigns the name of this bicycle's owner
	public void setOwnerName( String name ) {
	
		ownerName = name;
	}   

	// Returns the license number of this bicycle
	public int getLicenseNumber( ) {
		
		return licenseNumber;
	}

	// Assigns the license number of this bicycle
	public void setLicenseNumber( int number ) {
	
		licenseNumber = number;
	}	
 
 }


ok so now i need to add a no argument constructor to this class. what is the best way to go about doing this?

This post has been edited by pyr0b0y: 23 February 2009 - 12:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: No Argument Constructor

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: No Argument Constructor

Posted 23 February 2009 - 12:23 AM

public Bicycle() {}

or better

public Bicycle() {
	this.ownerName = "";
	this.licenseNumber = 0;
}


This post has been edited by n8wxs: 23 February 2009 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: No Argument Constructor

Posted 23 February 2009 - 12:39 AM

but if i already have a constructor
// Constructor
	public Bicycle( String name, int license ) {
		ownerName = name;
	  licenseNumber = license;
	}


can i still use
public Bicycle() {}


and by leaving it blank im initializing both the String and int right. is it the same as saying
public Bicycle(String name, int number) {}


thanks

This post has been edited by pyr0b0y: 23 February 2009 - 12:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: No Argument Constructor

Posted 23 February 2009 - 12:56 AM

View Postpyr0b0y, on 22 Feb, 2009 - 11:39 PM, said:

but if i already have a constructor
// Constructor
	public Bicycle( String name, int license ) {
		ownerName = name;
	  licenseNumber = license;
	}


can i still use
public Bicycle() {}


and by leaving it blank im initializing both the String and int right. is it the same as saying
public Bicycle(String name, int number) {}


thanks

No, it's not the same. public Bicycle() {} will create an instance of the class with the primitive instance variables initialized to zero. The string variables will be null.

This post has been edited by n8wxs: 23 February 2009 - 12:57 AM

Was This Post Helpful? 0
  • +
  • -

#5 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: No Argument Constructor

Posted 23 February 2009 - 01:00 AM

Yes you can have multiple constructors for a class. I'm not sure what you mean by

Quote

initializing both the string and the int

If you create an object (Bicycle) with the default constructor and leave it blank. The member variables of the class will have the default
values designated by the java language. In this case ownerName will have a value of null and licenseNumber will have a value of 0.

Generally it is a better idea to set the "default" value of your member variables in the constructor as n8wxs showed. After all that
is what the constructors sole purpose.
Was This Post Helpful? 0
  • +
  • -

#6 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: No Argument Constructor

Posted 23 February 2009 - 01:15 AM

but when i add
public Bicycle() {}

i get an error message that says
Bicycle is already defined in Bicycle

doesnt the second constructor need to be different from the first. the reason im asking is because the code above is set to just change the information, but i need to accept user input, so i need to add a no argument constructor to the Bicycle class. the constructor will initialize the owner's name to null and the license number to 0.

how would i go about doing that?
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: No Argument Constructor

Posted 23 February 2009 - 02:10 AM

the constructors are different, the first one will initialize the object with value that you will pass to the constructor..the second one will initialize them to their default values which are null for string and 0 for number. when you call the no arg constructor, you wont pass any args to it
Bicycle b = new Bicycle();


and when you call the other constructor
Bicycle a = new Bicycle("mountain bike", 123);



to change the values of your instance values, you can then use your methods provided in your class..hope this helps
Was This Post Helpful? 0
  • +
  • -

#8 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: No Argument Constructor

Posted 23 February 2009 - 08:10 AM

to initialize a constructor using no data you could do this
// Constructor
	public Bicycle() 
   {
		ownerName = "";
		licenseNumber = 0;
	}
// this sets both instance variables to null, then you can just set them later for this object


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1