Java error (basic) | Sololearn: Learn to code for FREE!


Java error (basic)

What is wrong in this code. I know this is simple and something is missing, but what.

5 Answers

New Answer


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); } }


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?


static String name; static int age; public static void main(String[] args) { System.out.println("Name is "+ name); System.out.println("Age is " + age); }


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(); = "Brian May"; //to-do write constructor for it p.age = 74; System.out.println( p ); } }


Try again and best of luck