Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person { // this is the class declaration
- // these are instance variables. any method in this class can access and change them
- public String name; // public means that anything can access this variable
- private int age; // private means that only methods in this class can access this variable
- // this is a default constructor. if no parameters are provided, this constructor is called
- public Person() {
- name = "Jimmy";
- age = 10;
- }
- // if this object is created with the following parameters, this constructor is called
- public Person(String newName, int newAge) {
- name = newName;
- age = newAge;
- }
- public void PrintHello() { // this is a method of the class
- System.out.println("Hello, " + name + "!");
- }
- public int HighFive() { // this is another method. it returns the number 5
- return 5;
- }
- public int GetAge() {
- return age; // even though age is private, we can share its value by using a method
- }
- // let's add two numbers and return the sum
- public int AddNumbers(int a, int b) {
- int c = a + b;
- return c;
- }
- }
Add Comment
Please, Sign In to add comment