class and object example
Example 1: how to make an objec tjava
class Puppy() {
String name;
int age;
Puppy(String puppy_name, int puppy_age) {
name = puppy_name;
age = puppy_age;
}
public static void main(String[] args) {
Puppy rex = new Puppy("rex",4);
}
}
Example 2: classes in java
// this might help you understand how classes work
public class MathTest {
public static void main(String[] args) {
class MathAdd {
int num1;
int num2;
public int addNumbers() {
int addThemUp = num1 + num2;
return addThemUp;
}
}
MathAdd addition = new MathAdd(); // create a new instance of the class
// you can access variables from the class
addition.num1 = 10;
addition.num2 = 20;
// and use the method from the class to add them up
System.out.println(addition.addNumbers());
}
}