0

If I create a class to be used by an application how can I display some of the information from the class. I am using get and set for the information but as soon as I add the public static void line so I can have a display method everything screws up. Do I sound lost? I am.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by peter_budo
0

Very simple create instance and initialize the class that will be your data holder, then trough instance access any available methods and retrieve data as need it. Bellow example should be sufficient

public class Person{
	
	private String firstName, lastName;
	
	public Person(String _firstName, String _lastName){
		setFirstName(_firstName);
		setLastName(_lastName);
	}
	
	private void setFirstName(String _firstName){
		firstName = _firstName;
	}
	
	public String getFirstName(){
		return firstName;
	}
	
	private void setLastName(String _lastName){
		lastName = _lastName;
	}
	
	public String getLastName(){
		return lastName;
	}
}
public class PersonTest{
	
	public static void main(String[] args){
		Person person = new Person("peter","somebody");
		System.out.println("First name : "+ person.getFirstName() 
			+ "\nLast name : " + person.getLastName());
	}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.