+ 3
Java error (basic)
What is wrong in this code. I know this is simple and something is missing, but what. https://code.sololearn.com/c0DwIVlq4aZ2/?ref=app
5 ответов
+ 8
Anshika Singh
As Denise has explained you can make that method as constructor or by creating an object.
Currently in your code yoy have created an method inside class but not passed any argument or paremeters to that class which is first issue to be fixed. You need ti call the created function to be worked so call the function via creating an main function below is the fixed code attached...
If you wanted to create via concept of object or constructor than you can update accordingly as above answer by Denise...
class Program {
static int display_details(String name, int age) {
System.out.println("Name is "+ name);
System.out.println("Age is " + age);
return 0;
}
public static void main(String[ ] args) {
display_details("A",4);
display_details("B",55);
}
}
+ 3
Anshika Singh
It depends on what you want to do.
You can add the main method inside the Program class which calls displayDetails (Java uses camelCase for method names)
or you write another class with the main in it.
In this case you need to create an object of Program to call displayDetails.
And you need do decide how name and age gets their values.
Via constructor or via method or directly?
+ 2
static String name;
static int age;
public static void main(String[] args) {
System.out.println("Name is "+ name);
System.out.println("Age is " + age);
}
+ 2
standard way is to use toString()
public class Person {
public String name;
public int age;
public String toString() {
return "Name is " +name +"\n"+
"Age is " +age;
}
}
public class Program {
public static void main(String[] args) {
Person p = new Person();
p.name = "Brian May"; //to-do write constructor for it
p.age = 74;
System.out.println( p );
}
}
+ 1
Try again and best of luck