public class Dog {
// Instance variable
public int weightInPounds;
// Constructor
public Dog(int startingWeight) {
weightInPounds = startingWeight;
}
// Non-static method (Instance method)
public void makeNoise() {
if (weightInPounds < 10) {
System.out.println("yipyipyip!");
} else if (weightInPounds < 30) {
System.out.println("bark. bark.");
} else {
System.out.println("woof!");
}
}
}
public class DogLauncher {
public static void main(String[] args) {
Dog smallDog; // Declaration
new Dog(20); // Instantiation
smallDog = new Dog(5); // Instantiation and Assignment
Dog hugeDog = new Dog(150); // Declaration. Instantiation, and Assignment
smallDog.makeNoise(); // Invocaton
hugeDog.makeNoise(); // The dot notation
}
}
Dog[] dogs = new Dog[2];
dogs[0] = new Dog(8);
dogs[1] = new Dog(20);
dogs[0].makeNoise();
public static Dog maxDog(Dog d1, Dog d2) {
if (d1.weightInPounds > d2.weightInPounds) {
return d1;
}
return d2;
}
public Dog maxDog(Dog d2) {
if (this.weightInPounds > d2.weightInPounds) {
return this;
}
return d2;
}
public static String binomen = "Cans famliiaris";
public class ArgsDemo {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
$ java ArgsDemo these are command line arguments
these