Results 1 to 4 of 4
- 11-01-2010, 10:58 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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?
ThanksLast edited by liquidtoon; 11-01-2010 at 11:30 AM.
- 11-01-2010, 11:50 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You need to create an instance of Name and then call the methods on that.
(Name kjbdkcb = new Name(...insert parameters here...))
- 11-01-2010, 12:24 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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:
I've tried every which way and can't do it, do you have any pointers?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);
}
- 11-01-2010, 01:19 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Basic GUI question (showing info from another class)
By alacn in forum New To JavaReplies: 10Last Post: 06-17-2010, 04:22 AM -
How it is possible view info about class?
By artemff in forum New To JavaReplies: 3Last Post: 12-31-2009, 12:17 AM -
[SOLVED] how to print a class that extends another class
By alpdog14 in forum New To JavaReplies: 3Last Post: 03-19-2009, 05:00 PM -
Beginner; Create a class to store info and constructor to initialize
By badness in forum New To JavaReplies: 16Last Post: 05-08-2008, 09:45 PM -
Getting Class info
By Java Tip in forum Java TipReplies: 0Last Post: 12-06-2007, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks