Rakesh Sheshadri Rakesh Sheshadri - 5 days ago 6
Java Question

Not able to execute this program in Eclipse. If im able to execute it what are the changes needs to be done?

//This class defines person.
Class person {
String name;
int age;

//This class calls method talk.
void talk(){
System.out.println("hello is:" +name);
System.out.println("my age is :" +age);
}
}

Answer

You need a main method. Java always seeks for a main method to run, the signature is always the same: public static void main(String[] args);

Class person {

    String name;
    int age;

    public person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //This class calls method talk.

    public void talk(){
        System.out.println("hello is:" +name);
        System.out.println("my age is :" +age);
    }

    public static void main(String[] args) {
        person myperson = new person("foobar", 21);
        myperson.talk();
    }

}
Comments