Results 1 to 4 of 4
  1. #1
    liquidtoon is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Print info from a different class?

    Hi. I'm just a beginner for JAVA and I hope you can help.

    I am trying to print the information from the following code:

    /**
    * Represents a name.
    *
    * @author Ian Bradley
    * @version 04-02-2009
    */
    public class Name
    {
    private String firstName;
    private String secondName;
    private String lastName;

    /* No default Constructor for objects of class Name.
    * You can't logically have a Name that doesn't that doesn't exist yet
    */

    /**
    * Constructor for objects of class Name
    * Only a first name and last name
    */
    public Name(String firstName, String lastName)
    {
    this.firstName = firstName;
    this.lastName = lastName;
    }

    /**
    * Constructor for objects of class Name
    * first name, second name and last name
    */
    public Name(String firstName, String secondName, String lastName)
    {
    this.firstName = firstName;
    this.secondName = secondName;
    this.lastName = lastName;
    }

    /**
    * returns the first name
    *
    * @return the first name
    */
    public String getFirstName()
    {
    return firstName;
    }

    /**
    * returns the second name
    *
    * @return the second name
    */
    public String getSecondName()
    {
    return secondName;
    }

    /**
    * returns the last name
    *
    * @return the last name
    */
    public String getLastName()
    {
    return lastName;
    }

    /**
    * set the first name
    *
    * @param first the first name
    */
    public void setFirstName( String firstName )
    {
    this.firstName = firstName;
    }

    /**
    * set the second name
    *
    * @param second the second name
    */
    public void setSecondName( String secondName )
    {
    this.secondName = secondName;
    }

    /**
    * set the last name
    *
    * @param last the last name
    */
    public void setLastName( String lastName )
    {
    this.lastName = lastName;
    }

    /**
    * returns the full name name
    *
    * @return the full name name
    */
    public String getFullName()
    {
    String name = "";
    if ( firstName != null )
    name = name + firstName;
    if ( secondName != null )
    name = name + " " + secondName;
    if ( lastName != null )
    name = name + " " + lastName;
    return name;
    }

    }

    and I have this in my other class:
    private void printCustomerName()
    {
    system.out.print(Name.getFullName());
    }

    the error that comes up is:

    "Non-static method getFullName() cannot be referenced from a static context"


    Can you advise what I am doing wrong?

    Thanks
    Last edited by liquidtoon; 11-01-2010 at 11:30 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    You need to create an instance of Name and then call the methods on that.
    (Name kjbdkcb = new Name(...insert parameters here...))

  3. #3
    liquidtoon is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks alot!

    Sorry to be a pain, but I'm having another bit of trouble.

    I am trying to do a System.out.print for only firstName and lastName of the following method:

    private void createBooking()
    {
    String title;
    String firstName;
    String lastName;
    String bookingNo;
    String roomType;

    System.out.print("Enter title: ");
    title = keyboard.next();
    System.out.print("Enter first name: ");
    firstName = keyboard.next();
    System.out.print("Enter last name: ");
    lastName = keyboard.next();
    System.out.print("Enter booking number: ");
    bookingNo = keyboard.next();
    System.out.print("Enter room type: ");
    roomType = keyboard.next();

    // Create the Booking object
    aBooking = new Booking (title, firstName, lastName, bookingNo, roomType);

    }
    I've tried every which way and can't do it, do you have any pointers?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    That doesn't seem to bear any relation to the first question?
    And also makes little sense...assuming you know something about Java:
    print(firstName + " " + lastName);

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2010, 04:22 AM
  2. How it is possible view info about class?
    By artemff in forum New To Java
    Replies: 3
    Last Post: 12-31-2009, 12:17 AM
  3. Replies: 3
    Last Post: 03-19-2009, 05:00 PM
  4. Replies: 16
    Last Post: 05-08-2008, 09:45 PM
  5. Getting Class info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:46 PM

Posting Permissions

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