DEV Community

Arshad Ali
Arshad Ali

Posted on

JAVA- Constructors

main.java



    public class Main {

        public static void main(String[] args) {

        Human human1 = new Human("Rick", 65,70);
        Human human2 = new Human("Morty", 16,50);

        //System.out.println(human1);
        //System.out.println(human2);

        human1.eat();
        human2.drink();
    }           

}
Enter fullscreen mode Exit fullscreen mode

class.java


public class Human {

    String name;
    int age;
    double weight;

    Human(String name, int age, double weight){

        this.name = name;
        this.age = age;
        this.weight = weight;
    }

    void eat() {
        System.out.println(this.name+" is eating");
    }
    void drink() {
        System.out.println(this.name+" is drinking *burp*");
    }

}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)