class Dog { // this is Class
int age;
public Dog(int dogsAge) { // what we call this line?
dogsAge = age; // why are we defining dogsAge = age; ?
}
public void bark(){ // what we call this line?
System.out.println("woof!");
}
public void run(int feet){
System.out.println("your dog ran " + feet +" feet!");
}
public static void main(String[] args) {
Dog spike = new Dog(5);
spike.bark();
spike.run(35);
}
sorry if this sounds stupid. I never got about class, object. if someone can explain, It would really be grateful.
its the constructor, its a method but its automatically called when you create an instance of your class.
here:
Dog spike = new Dog(5);
you create an instance named spike, which will cause the constructor to execute.
age is instance variable, it has a different value for each instance, so when we create a new Dog(argument); we pass an argument (5 for spike) to the constructor, so we can set the instance variable.
a public method which doens’t return anything (void) named bark