Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Change class type with constructor

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Thanked 0 Times in 0 Posts

    Default Change class type with constructor

    So I changed String address to Address address with another class type I have declared. However my constructor can't accept the string for the address anymore. How do I initialize my address values for the objects I create with the constructor. My constructor is listed below.

    public Person(int id, String firstName, String lastName, Address address, long phone) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.phone = phone;

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,635 Times in 2,585 Posts

    Default Re: Change class type with constructor

    my constructor can't accept
    If there are error messages, please copy the full text and paste it here.
    Also post the code that defines the variables defined in the class.

    Please edit your post and wrap your code with code tags:
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Thanked 2 Times in 2 Posts

    Default Re: Change class type with constructor

    You have to create an Address instance, and then pass it to the Person class' constructor.
    For example:
    Address address = new Address("City name", "Street, house number, etc.");
    Person person = new Person(1, "John", "Doe", address, 1234567L);
    The constructor of the Address may be different than I used in the example.

Similar Threads

  1. [SOLVED] how to convert a input array of type Strings to a class type
    By adit in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 17th, 2014, 07:46 PM
  2. Replies: 2
    Last Post: June 9th, 2014, 04:06 PM
  3. How to avoid a constructor of class A be extended to Class B?
    By chaitra1987 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 28th, 2013, 11:46 PM
  4. Change HashMap Type
    By Purple01 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 14th, 2012, 07:23 AM
  5. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM

Tags for this Thread